Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

What's the proper way to use JSON.stringify() on nested objects? I'm getting a bad request from a api call that expects a JSON string:

testData = {"credentials": {"user": "[email protected]", "password": "testpassword", "country": "us"}};

when I view the postDasta:

"{"credentials": {"user": "[email protected]", "password": "testpassword", "country": "us"}}";

$.ajax({
    ...
    data: JSON.stringify(testData),
    ...
});

Thanks,

J

share|improve this question
    
Have you tried passing the object to the data without stringifying it? I don't quite remember the data parameter expecting a string. It either, as far as I remember, expects an object, or a string in lieu of a get request. – Daedalus Oct 29 '13 at 0:34
up vote 2 down vote accepted

The answer I was looking for was: You need to use JSON.stringify to first serialize your object to JSON, and then specify the content-type so your server understands it's JSON.

So, contentType and stringify are necessary if the server is expecting JSON.

share|improve this answer

If this is jQuery (it looks like it is), the data parameter accepts an object and performs the necessary serialization to pass it as an associative array to the server. You don't stringify if before passing it.

share|improve this answer
    
if I leave off contentType:'application/json' I get: 415 Unsupported Media Type - 30ms - 752B). When I use a rest client the request works fine with {"credentials": {"user": "[email protected]", "password": "testpassword", "country": "us"}}. – neridaj Oct 29 '13 at 22:56
    
@neridaj How can you expect me to respond if you don't reply on my answer? – Daedalus Oct 29 '13 at 23:13

The data parameter for jQuery's .ajax() method does not take a json string as value. It either expects a query-formed string(test=value&otherstuff=othervalue), or an object, as noted in the linked documentation above.

share|improve this answer
    
I removed JSON.stringify() and verified that I'm sending over an object with $.isPlainObject(testData): true. When I view the postData it looks like a url encoded querystring: "credentials%5Buser%5D=test%40email.com&credentials%5Bpasswo‌​rd%5D=testpassword&c‌​redentials%5B&creden‌​tials%5Bcountry%5D=u‌​s" but the contentType is set to 'application/json' and the type is set to 'post'. – neridaj Oct 29 '13 at 19:16
    
var testData = {"credentials": {"user": "[email protected]", "password": "testpassword", "country": "us"}}; $.ajax({ url : '/credentials', type : 'post', contentType : 'application/json', success: function(data, textStatus, jqXHR) { alert('success'); }, data : testData, error : function(jqXHR, textStatus, errorThrown){ alert('error'); } }); – neridaj Oct 29 '13 at 19:20
    
You are not supposed to set the content type to application/json. I don't really see a reason to use it, unless you're specifically sending over json. As noted in the documentation, it converts any json object into a query string, so it can be converted by the server into a post request(this is done auto), eg.. without modifying the content type, it is retrievable with $_POST. – Daedalus Oct 29 '13 at 20:48
    
@neridaj You need to describe exactly what you are trying to achieve if you want an answer that solves your problem. As it stands, I have no issues executing what I told you. No errors, etc. – Daedalus Oct 29 '13 at 23:23
    
Sorry for posting under the wrong response. Basically, I'm not sure why the testData works in a rest client but not when it's executed within the $.ajax() context. This only happens when testData contains a nested object. – neridaj Oct 30 '13 at 0:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.