Ajax POST calls fail when I assign low-to-complex JavaScript objects to the [data] key/value pair after being serialized using JSON.stringify().
Question – What additional ajax call configuration is needed to successfully send a POST message with JavaScript object serialized with JSON.stringify()?
I strongly suspect some small configuration item is missing, but I have not been able to identify the issue in my research and testing. The call fails on the simplest of Javascript types, e.g., Objects, Arrays, etc.
Verified the remote web service can respond to POST calls with simple data.
Verified the remote web service can respond to POST calls with at least 22MB of simple data.
The same error behavior is observed across different browsers.
Referencing the code snippet, [MSG No. 1] always succeeds.
Referencing the code snippet, [MSG No. 2] always fails.
function stackoverflowAjaxPostCall() {
// The remote service is a WCF (REST) web service and performs as expected with the exception
//var url = "http://localhost/GenericWebService/GenericWebService.svc/SaveChanges";
// MSG No. 1 - This message works reliably and verified to work end-to-end with 22MB of "remarks" info
var messageToBeSent = "{ name: 'PostMyChanges', remarks: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}";
// MSG No. 2 - Simple Javascript object reliably fails with Client Side Error 400 (Bad Request)
// Stringify Output = {"name":"PostMyChanges","remarks":"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}
//var messageToBeSent = new Object();
//messageToBeSent.name = 'PostMyChanges';
//messageToBeSent.remarks = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var serializedJson = JSON.stringify(messageToBeSent);
console.log(serializedJson);
console.log("Data Size: " + serializedJson.length);
$.ajax(url,
{
type: "POST",
contentType: "application/json",
dataType: "json",
data: serializedJson,
//contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15", // Fails with or without
success: function (response) { console.log("Success: " + response); },
error: function (e) {
var inspectIt = e;
console.log("POST Call - Error occurred");
console.log("Response Length: " + e.responseText.length);
}
}
);
// Successful Remote Service Call Response = "{name:'SavedChanges',remarks:'Reached REST service entrance'}";
}