1

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?

4
  • which quotes and where? can you please write that string? Commented Dec 26, 2013 at 17:28
  • When you called stringify you 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. Commented Dec 26, 2013 at 17:29
  • @jeewan The quotes when storing to variable. See answer. Commented Dec 26, 2013 at 17:33
  • I think this is very similar to this stackoverflow.com/a/20571576/1310070 Commented Dec 26, 2013 at 17:34

2 Answers 2

2

You don't need to send the data as a string, jQuery will do that for you. Try this

var myData = {
  assessmentId: 1,
  pageId: 1,
  currentPage: 2
};

$.ajax({
    url: url,
    type: 'POST',
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    data: myData, //Notice the change here
    async: false,
    success: function(data) {
      // TO DO
    }
});

PS: There is a very similar answer here. Please check that.

Sign up to request clarification or add additional context in comments.

4 Comments

I need to send a variable in data's value like this: data : myJsonData, I already mentioned above that, the code you wrote already working.
@jeewan Take a look at updated answer. You are making an error by wrapping json in quotes
I tried this but you missed to stringify myData before sending. So, finally, this worked: myData = JSON.stringify(myDaya); Thanks :-)
Glad that you could find out yourself even with very small help..:)
1

You don't need the quotes when storing in variable.

var jsonSendingData = {
    assessmentId:1,
    pageId:1,
    currentPage:2
};

jQuery.ajax({
       url:url,
       type:'POST',
       data:jsonSendingData
  }).done(function(data){
   console.log(data);
 });

Otherwise you have to specify it in the request headers

var datastring = JSON.stringify(jsonSendingData);

data: datastring,
contentType: "application/json",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.