Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to put some JSON formatted data via Ajax with jQuery to a server. My code looks like this:

$.ajax({
    type: "PUT",
    url: myURL,
    contentType: "application/json",
    data: {"data": "mydata"}
});

But on the server-side, I receive a data=mydata string, instead of the expected JSON. Firebug tells me the same.

Where is the error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
698 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think the data needs to be a String. Objects are converted to query strings which is what you are seeing here.

You can use the JSON.stringify(obj) method to convert your Object to a String. The code for the JSON object is available from: https://github.com/douglascrockford/JSON-js/blob/master/json2.js.

Alternately, just pass the code you are using to create the object as a literal String, but I imagine this is just an example and you'll want to encode some object you've already created.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...