I need to send the JSON data in a jQuery's ajax call like this:
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: '{"assessmentId":1,"pageId":1,"currentPage":2}',
async: false,
success: function(data) {
// TO DO
}
});
This is working fine but the data I am sending needs to be stored in a variable like this:
var jsonSendingData = '{"assessmentId":1,"pageId":1,"currentPage":2}';
Once I keep it in a variable jsonSendingData and pass in ajax call, it gives me error.
I also tried to stringify the json data like this:
var jsonSendingData = JSON.stringify([{"assessmentId":1,"pageId":1,"currentPage":2}]);
It does not work. What am I doing wrong here?
stringifyyou added an extra pair of array-brackets on the outside. You're stringifying[{...}]instead of{...}and that difference will be faithfully passed on to the server. Apparently the server doesn't like it.