Rol*_*olf 28 javascript ajax jquery json
我希望这很清楚,我已经浪费了很多精力来解决这个问题,所以我可能没有多少留下来写一个完美的问题.此外,这可能要进入一个jQuery bug报告,但我宁愿它张贴在这里首先作为我是一个相对的JavaScript新手,所以也许我做错了什么?
我创建了以下代码来重现它.它使用PHP将收到的数据回显给浏览器,尽管它可以在没有任何PHP的情况下工作.
问题可以在Firefox 4和Chrome 10中重现.您需要控制台才能看到发生了什么.
这是代码:
<?
$input = file_get_contents('php://input');
if (isset($input) and !empty($input)) {
echo $input;
die();
}
?>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script>
$(function(){
var jsonData = {
"something":"??"
};
jsonData = JSON.stringify(jsonData);
var onSuccess = function(data){
console.log("Ajax Success!");
console.log(data);
}
var onError = function(jqXHR, textStatus, errorThrown){
console.log("Ajax Error: "+textStatus);
console.log("More info:");
console.log(errorThrown);
console.log(jqXHR);
}
console.log("Now sending this: "+jsonData+" through Ajax...");
var ajaxCmd = {
"url" : "test.php",
"dataType": "json",
"type" : "POST",
"async" : false,
"error" : onError,
"success" : onSuccess,
"data" : jsonData
};
$.ajax(ajaxCmd);
});
</script>
</head>
<body>
<pre>Check your JavaScript console...</pre>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
加载时,它会抛出一些明显不相关的解析错误或异常(取决于浏览器).应该发送的json是{"something":"??"}但如果你在Firebug的网络选项卡(或Chrome等价物)中检查它,你会看到"??" 正在被一些jQuery字符串取代,看起来像:jQuery152026845051744021475_1303152126170
这就是服务器收到的内容.
只有在发送的JSON对象内的值字段中有两个或多个连续的问号时才会出现此问题,即使其中有其他字母也是如此.有一个问号似乎有效.将"dataType"更改为"text"也可以解决此问题.但我需要所有这些功能!
如果你注释掉"jsonData = JSON.stringify(jsonData);" 或"$ .ajax(ajaxCmd);" 错误也奇迹般地消失了.
更多信息:
Chrome控制台输出:
test.php:21Now sending this: {"something":"??"} through Ajax...
jquery.min.js:16Uncaught SyntaxError: Unexpected token :
test.php:16Ajax Error: parsererror
test.php:17More info:
test.php:18jQuery15206220591682940722_1303153398797 was not called
test.php:19
Object
Run Code Online (Sandbox Code Playgroud)
Firefox Firebug输出:
Now sending this: {"something":"??"} through Ajax...
Ajax Error: parsererror
More info:
jQuery15206494160738701454_1303153492631 was not called
Object { readyState=4, responseText="{"something":"jQuery152...8701454_1303153492631"}", more...}
invalid label
{"something":"jQuery15206494160738701454_1303153492631"}
Run Code Online (Sandbox Code Playgroud)
Jus*_*ijn 25
jQuery在使用时??用作回调函数的占位符jsonp.当它解析Ajax请求并找到双重问号(或更多问号)时,它会自动假定您正在尝试使用jsonp.手动设置内容类型时,它将忽略问号.
因此,使用contentType避免此问题:
$.ajax(
url: "your-url.php",
dataType: "json", // what you expect the server to return
contentType: "application/json", // what you are sending
...
);
Run Code Online (Sandbox Code Playgroud)
以供参考:
jQuery Bugtracker:$ .AJAX改变帖子内容如果包含"??" (2问号)
希望它可以节省其他人的数小时调试...
如果您不打算将"数据"值格式化为有效的HTML查询字符串,则不应对其进行预先字符串化.正如您所指出的,如果您不调用"JSON.stringify()",那么它可以工作.那是因为图书馆已经知道要为你处理.
现在,如果您想将JSON字符串作为参数本身发送到需要解码某些JSON的服务器端代码,那么您需要将其设置为参数:
$.ajax(url, {
// ...
data: { jsonParam: jsonData },
// ...
});
Run Code Online (Sandbox Code Playgroud)
现在,您的服务器将看到一个带有名为"jsonParam"的参数的HTTP请求,其值将是您的JSON字符串字符串.
| 归档时间: |
|
| 查看次数: |
6736 次 |
| 最近记录: |