页面
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 });
{"NumberOfcpus":"2","OSVersion":"jQuery19107363727174233645_1373301489648?"}
怎么了?是字符串“???” jQuery中某种特殊控制词或某些东西?@H_301_5@
谢谢.@H_301_5@
解决方法
如果我仔细阅读其他答案中的错误评论,真正的问题在于,在将json-string传递给数据之前将其字符串化.数据需要一个包含查询的字符串(例如“a = asdf& b = qwer”)或包含它将成为查询的键和值的Object.而是传递类似'{“a”:“asdf”,“b”:“qwer”}’的字符串,包含不是查询的字符串化数组.它以某种方式设法转换服务器理解的数据,但显然也会触发该错误/功能.
如果要通过$_POST [‘a’]访问数据,以获取JSON对象中的键“a”:@H_301_5@
$.ajax( { async: false,data: model,//Pass the Array itself to the function dataType: "json",url: serverSideURL });
(Source; Cowboy on jQuery bugs)@H_301_5@
如果要检索JSON字符串:@H_301_5@
$.ajax( { async: false,data: { "MyLilString": JSON.stringify( model ) } dataType: "json",url: serverSideURL });
在这种情况下,$_POST [“MyLilString”]包含序列化的JSON,您可以使用json_decode().@H_301_5@
(Source; Ajpiano on jQuery bugs)@H_301_5@
另一个消化@H_301_5@
我从评论中提取的另一个建议(但我现在无法找到-_-‘)是将jsonp设置为false.@H_301_5@
$.ajax( { async: false,data: model dataType: "json",url: serverSideURL,jsonp: false });