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?
Share Improve this question edited Apr 30, 2014 at 5:54 user3391564 asked Apr 30, 2014 at 5:06 user3391564user3391564 5761 gold badge5 silver badges16 bronze badges 7- Send the obj as JSON.stringify(obj). If required, deserialize the same at server side or some html decode stuff. – d-coder Commented Apr 30, 2014 at 5:13
- How does the server want to get it? You could send the data as JSON rather than x-www-form-urlencoded ... benjamin-schweizer.de/jquerypostjson.html -- And here's a little more background reading: stackoverflow./questions/2845459/… – mgilson Commented Apr 30, 2014 at 5:16
- @mgilson the server wants to get it as raw, unescaped json, like {"firstname": "bill"... etc., though that could change if necessary. I'll try the code in the blog post. – user3391564 Commented Apr 30, 2014 at 5:21
- check the answer here : stackoverflow./questions/16574482/… – dev1234 Commented Apr 30, 2014 at 5:24
-
1
@user3391564 -- I doubt it gets an empty request. The problem is that most frameworks are expecting x-www-form-urlencoded data. If you actually look at the request body, the JSON will be in there (which you may need to parse yourself). I recently ran into this problem when dealing with requests from angular's $http (since it posts the json as
application/json
which actually makes some sense...) – mgilson Commented Apr 30, 2014 at 5:37
2 Answers
Reset to default 1From 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
});