Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

jQuery - How to PUT JSON via Ajax?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...