Sending JSON content type in from JavaScript objects ( jQuery ajax )
Posted
by
smartnut007
on Stack Overflow
See other posts from Stack Overflow
or by smartnut007
Published on 2012-06-01T21:31:40Z
Indexed on
2012/06/01
22:40 UTC
Read the original article
Hit count: 175
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);
}
});
© Stack Overflow or respective owner