Sending JSON content type in from JavaScript objects ( jQuery ajax )
- by smartnut007
I am having trouble sending JavaScript objects as a http request that only accepts json content-type. i.e "application/json" or "text/json"
But, I am not sure why
data2 ( stringified json ) works fine
But, data1 ( json object ) does throws 400 ( Bad Request ).
i.e I am not sure jQuery does not serialize the json object to a valid json string for the server to process.
var data1 = ({ rating : 3 }); //does not work
var data2 = '{ "rating" : 3}'; //works fine
$.ajax({
url : "/rate",
data : data1,
type : "POST",
contentType: "application/json",
success: function(json){
console.log("Ajax Return :"+json);
}
});