I'm using Node + Express
I use the following function to fetch some json from the server:
$.ajax({
url: url,
contentType: "application/json",
dataType: "json",
type: "POST",
data: JSON.stringify( data ),
success: cb
});
About a 1/3 of the time Express fires back an error:
{ [Bad Request: Bad Request] name: 'Bad Request', status: 400 }
Any ideas what I might be doing wrong?
It sounds like about 1/3 of the time, your data isn't an object or an array (but perhaps a single number or string, or null or undefined).
Even though JSON.stringify will handle those types, they don't produce a valid JSON text (as it's called in RFC4627, which defines the JSON format).
I'd suggest you check your data before converting it into json, for whether it is already json or not. See this answer for checking json.