Solved. The solution is to set contentType to 'application/json' and use the JSON.stringify(obj) instead of obj, but you may then have to change how you get the data on your server, depending on the language or framework. Original question below...
Here's what I'm trying
var obj = {
'firstName': 'bill',
'lastName': 'johnson',
'hobbies': ['apples', 'dogs']
});
$.ajax({
type: 'POST',
url: '/myurl'
data: obj,
success: function(data){alert(data);}
});
If I alert/log a JSON.stringify(obj)
, I get the correct result, i.e.:
{'firstName': 'bill', 'lastName': 'johnson', 'hobbies': ['apples', 'dogs']}
However, when I do the above ajax call, my server gets the following:
{'firstName': 'bill', 'lastName': 'johnson', 'hobbies[]': 'apples'}
Which clearly is not proper json. I've tried adding various contentType
arguments but then my server actually gets nothing (an empty post request).
I also tried setting the data argument to a pre-stringified string of JSON (which is correct), but then jquery escapes it and my server gets this:
{"{\"firstName\":\"bill\",\"lastName\":\"johnson\",\"hobbies\":[\"apples\",\"dogs\"]}": ""}
I tried setting processData
to false
and that changes nothing.
I've researched this for hours and haven't gotten it to work. Surely there's a way to send json with lists to the server...
any tips?
Solved. The solution is to set contentType to 'application/json' and use the JSON.stringify(obj) instead of obj, but you may then have to change how you get the data on your server, depending on the language or framework. Original question below...
Here's what I'm trying
var obj = {
'firstName': 'bill',
'lastName': 'johnson',
'hobbies': ['apples', 'dogs']
});
$.ajax({
type: 'POST',
url: '/myurl'
data: obj,
success: function(data){alert(data);}
});
If I alert/log a JSON.stringify(obj)
, I get the correct result, i.e.:
{'firstName': 'bill', 'lastName': 'johnson', 'hobbies': ['apples', 'dogs']}
However, when I do the above ajax call, my server gets the following:
{'firstName': 'bill', 'lastName': 'johnson', 'hobbies[]': 'apples'}
Which clearly is not proper json. I've tried adding various contentType
arguments but then my server actually gets nothing (an empty post request).
I also tried setting the data argument to a pre-stringified string of JSON (which is correct), but then jquery escapes it and my server gets this:
{"{\"firstName\":\"bill\",\"lastName\":\"johnson\",\"hobbies\":[\"apples\",\"dogs\"]}": ""}
I tried setting processData
to false
and that changes nothing.
I've researched this for hours and haven't gotten it to work. Surely there's a way to send json with lists to the server...
any tips?
application/json
which actually makes some sense...)
– mgilson
Commented
Apr 30, 2014 at 5:37
From your post that looks correct to me, I am somewhat new to JSON myself but it looks like its treating the last key-value pair as an array, to access the individual elements you would have to use the correct index to access the value. from json JSON is built on two structures: •A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. •An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence. you could check it on jsonlint. if you still think something is wrong.
Try this
$.ajax({
url: url,
type: 'POST',
datatype: "json",
traditional: true,
data: {
menuItems: JSON.stringify(myArray),
},
success: function () { window.alert('success'); },
error: function (event, request, settings) {
window.alert('error'); },
timeout: 20000
});