2

I try to send list of object like [{"name":"Vasya"},{"name":"Lila"}]

It's my code:

$.ajax({
    url: url
,   type: 'POST'
,   contentType: 'application/json'
,   data: data
,   success:  function(response) {
        showPopup(response.successMessage);
    }
});

Where alert(JSON.stringify(data)); shows: [{"name":"Vasya"},{"name":"Lila"}]

But when I am checking my request in chrome debug mode the request contains undefined= instead correct data.

What I do wrong? Is this syntax incorrect according to JSON?

4
  • 1
    Pretty offensive user name. Commented Jan 31, 2015 at 15:16
  • 2
    @mplungjan was that supposed to be sarcastic? Or did you really mean it? Commented Jan 31, 2015 at 15:18
  • Remove the contentType option to send the data as x-www-form-urlencoded, or stringify the object as JSON to actually send JSON, now you're telling the server that whatever you're sending should be valid JSON, but you're sending x-www-form-urlencoded Commented Jan 31, 2015 at 15:18
  • @adeneo shouldn't the request at least contains name=Vasya&name=Lila though? OP mentioned Chrome is sending it as undefined instead. Commented Jan 31, 2015 at 15:21

1 Answer 1

1

You need to stringify the object when sending it so that you send JSON.

, data: JSON.stringify(data)
Sign up to request clarification or add additional context in comments.

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.