页端JavaScript:
VAR模型= {NumberOfCPUs:2,NumberOfCores:4,OSTYPE:Linux的,OSVERSION:???} ;
变种的id = 0;
VAR成功= FALSE;
//发送到服务器:
$阿贾克斯(
{
异步:假的,
数据:JSON.stringify(模型),
数据类型:JSON,
类型:后,
网址:serverSideURL
});
的请求的实际被发送到服务器的内容:
{"NumberOfCPUs":"2","NumberOfCores":"4","OSType":"Linux","OSVersion":"jQuery19107363727174233645_1373301489648?"}
这是怎么回事?是字符串???某种jQuery的特殊控制字什么的?
感谢。
解决方案如果我通过在其他的答案中的错误的意见看,对的真正的问题在于,你字符串化的JSON它传递给数据之前-string
。 数据
预计,无论是字符串
包含查询(如一=航空自卫队和b = QWER
或对象
包含键和值,它会变成一个查询)。你是不是传递像'{一:ASDF,B:QWER}
,一个字符串
包含字符串化数组,它是不是一个查询。它以某种方式设法在服务器理解数据转换,但显然会触发这个bug /功能了。
解决方法1
如果您想通过 $来访问数据_ POST ['一']
来获得关键一
在您的JSON对象:
$。阿贾克斯({
异步:假的,
数据:模型,//通过阵列本身的功能
数据类型:JSON,
类型:后,
网址:serverSideURL
});
(来源;牛仔jQuery的错误的)
解决方案2
如果你想获取JSON字符串:
$。阿贾克斯({
异步:假的,
数据: {
MyLilString:JSON.stringify(模型)
}
数据类型:JSON,
类型:后,
网址:serverSideURL
});
在这种情况下, $ _ POST [MyLilString]
包含序列化的JSON,您可以使用 json_de code()
上。
(来源; Ajpiano jQuery的错误的)
另一个sugestion
另外一个建议,我的意见并提取(但我现在无法找到-_-')是设置 JSONP
到假
。
$。阿贾克斯({
异步:假的,
数据:模型
数据类型:JSON,
类型:后,
网址:serverSideURL,
JSONP:假的
});
这应该从插入回调函数到请求体内过多停止jQuery的。
Page side javascript:
var model = {"NumberOfCPUs":"2","NumberOfCores":"4","OSType":"Linux","OSVersion":"???"};
var id = 0;
var success = false;
//send to server:
$.ajax(
{
async: false,
data: JSON.stringify(model),
dataType: "json",
type: "post",
url: serverSideURL
});
The contents of the request that actually gets sent to the server:
{"NumberOfCPUs":"2","NumberOfCores":"4","OSType":"Linux","OSVersion":"jQuery19107363727174233645_1373301489648?"}
What is happening? Is the string "???" some sort of special control word or something in jQuery?
Thanks.
解决方案If I read through the comments of the bugs in the other answers, the real problem lies in the fact that you stringify your json-string before passing it to data
. data
expects either a String
containing a query (e.g. "a=asdf&b=qwer"
or an Object
containing the keys and values that it will turn into a query). You are instead passing something like '{"a":"asdf","b":"qwer"}'
, a String
containing a stringified array that is not a query. It somehow manages to convert in data the server understands, but apparently triggers that bug/feature too.
Solution 1
If you want to access the data via $_POST['a']
to get key "a"
in your JSON-object:
$.ajax( {
async: false,
data: model, //Pass the Array itself to the function
dataType: "json",
type: "post",
url: serverSideURL
});
(Source; Cowboy on jQuery bugs)
Solution 2
If you want to retrieve the JSON-string:
$.ajax( {
async: false,
data: {
"MyLilString": JSON.stringify( model )
}
dataType: "json",
type: "post",
url: serverSideURL
});
In this case $_POST["MyLilString"]
contains the serialized JSON, which you can use json_decode()
on.
(Source; Ajpiano on jQuery bugs)
Another sugestion
Another suggestion I did extract from the comments (but I am now unable to find -_-') is to set jsonp
to false
.
$.ajax( {
async: false,
data: model
dataType: "json",
type: "post",
url: serverSideURL,
jsonp: false
});
This should stop jQuery from inserting the callback function into the request body too.