jQuery的 - 如何通过Ajax PUT JSON? [英] jQuery - How to PUT JSON via Ajax?
问题描述
我试图把一些通过Ajax JSON格式的数据 jQuery的到服务器。我的code是这样的:
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"}
});
但在服务器端,我收到了数据= MYDATA
字符串,而不是预期的JSON。 萤火虫告诉我的一样。
But on the server-side, I receive a data=mydata
string, instead of the expected JSON. Firebug tells me the same.
在哪里错误?
推荐答案
我认为数据必须是一个字符串。对象转换为查询字符串是你在这里看到的是什么。
I think the data needs to be a String. Objects are converted to querystrings which is what you are seeing here.
您可以使用JSON.stringify(OBJ)方法的对象转换为字符串。在code的JSON对象可从:<一href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js">https://github.com/douglascrockford/JSON-js/blob/master/json2.js.
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.
另外,只是通过您使用创建对象作为一个字符串的code,但我想这只是一个例子,你要连接code您已经创建了一些对象。
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.
这篇关于jQuery的 - 如何通过Ajax PUT JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!