Well, here is the story:
故事是这样的:
I have some data need to send to server, but they should turned into JSON dataType first.
我有一些数据需要发送到服务器,但是它们应该首先转换成JSON数据类型。
I made such ajax call:
我打了一个ajax电话:
$.ajax({
url: url, // the url I want to post to.
type: 'POST',
contenttype:'application/json; charset=utf-8',
beforeSend: //some HTTP basic auth stuff
data: {
name:'test',
key:'foo',
key2:'bar'
},
dataType:'JSON'
});
basically I'm expecting the data I send to server was:
基本上我希望我发送给服务器的数据是:
[name:test,key:foo,key2:bar]
but what I've got was:
但我得到的是:
name=test&key=foo&key2=bar
What did I missing? How can I get those data into JSON?
我错过了什么?如何将这些数据转换成JSON?
6 个解决方案
#1
22
var data = {'bob':'foo','paul':'dog'};
$.ajax({
url: url,
type: 'POST',
contentType:'application/json',
data: JSON.stringify(data),
dataType:'json'
});
/** Added **/
/ * *添加* * /
The above does not do anything with the response from the server if you need to do something then a callback will be called when the server has responded.
如果您需要做一些事情,那么当服务器作出响应时,将调用回调。
var data = {'bob':'foo','paul':'dog'};
$.ajax({
url: url,
type: 'POST',
contentType:'application/json',
data: JSON.stringify(data),
dataType:'json',
success: function(data){
//On ajax success do this
alert(data);
},
error: function(xhr, ajaxOptions, thrownError) {
//On error do this
if (xhr.status == 200) {
alert(ajaxOptions);
}
else {
alert(xhr.status);
alert(thrownError);
}
}
});
#2
3
I've had the same problem. You can't send an object as "data", you need to stringify the object. Try this instead, with your object stringified:
我也遇到过同样的问题。不能将对象作为“数据”发送,需要对对象进行字符串化。试试这个,你的对象被绑定:
$.ajax({
url: url,
type: 'POST',
contentType:'application/json',
data: '{
name:"test",
key:"foo",
key2:"bar"
}',
dataType:'json'
});
#3
2
Try this: http://www.abeautifulsite.net/blog/2008/05/postjson-for-jquery/
试试这个:http://www.abeautifulsite.net/blog/2008/05/postjson-for-jquery/
Its a lot shorter:
它的很多短:
$.post(url, data, function(response) {
// Do something with the response
}, 'json');
#4
0
Also, is necesary can create a parameter and assign the value with JSON.stringify
另外,necesary可以创建一个参数并使用JSON.stringify来分配值。
....
data: "jsonString="+JSON.stringify(data),
...
#5
0
I agree the data must be converted into a JSON string, not only to agree with the dataType
and contentType
setup but more importantly, to satisfy the server.
我同意数据必须转换为JSON字符串,不仅要符合数据类型和内容类型设置,更重要的是,要满足服务器。
data: JSON.stringify(data),
dataType:'json'
#6
-3
dataType: 'json',
#1
22
var data = {'bob':'foo','paul':'dog'};
$.ajax({
url: url,
type: 'POST',
contentType:'application/json',
data: JSON.stringify(data),
dataType:'json'
});
/** Added **/
/ * *添加* * /
The above does not do anything with the response from the server if you need to do something then a callback will be called when the server has responded.
如果您需要做一些事情,那么当服务器作出响应时,将调用回调。
var data = {'bob':'foo','paul':'dog'};
$.ajax({
url: url,
type: 'POST',
contentType:'application/json',
data: JSON.stringify(data),
dataType:'json',
success: function(data){
//On ajax success do this
alert(data);
},
error: function(xhr, ajaxOptions, thrownError) {
//On error do this
if (xhr.status == 200) {
alert(ajaxOptions);
}
else {
alert(xhr.status);
alert(thrownError);
}
}
});
#2
3
I've had the same problem. You can't send an object as "data", you need to stringify the object. Try this instead, with your object stringified:
我也遇到过同样的问题。不能将对象作为“数据”发送,需要对对象进行字符串化。试试这个,你的对象被绑定:
$.ajax({
url: url,
type: 'POST',
contentType:'application/json',
data: '{
name:"test",
key:"foo",
key2:"bar"
}',
dataType:'json'
});
#3
2
Try this: http://www.abeautifulsite.net/blog/2008/05/postjson-for-jquery/
试试这个:http://www.abeautifulsite.net/blog/2008/05/postjson-for-jquery/
Its a lot shorter:
它的很多短:
$.post(url, data, function(response) {
// Do something with the response
}, 'json');
#4
0
Also, is necesary can create a parameter and assign the value with JSON.stringify
另外,necesary可以创建一个参数并使用JSON.stringify来分配值。
....
data: "jsonString="+JSON.stringify(data),
...
#5
0
I agree the data must be converted into a JSON string, not only to agree with the dataType
and contentType
setup but more importantly, to satisfy the server.
我同意数据必须转换为JSON字符串,不仅要符合数据类型和内容类型设置,更重要的是,要满足服务器。
data: JSON.stringify(data),
dataType:'json'
#6
-3
dataType: 'json',