我希望这足够清楚,我已经花了很多时间试图解决这个问题,所以我可能没有多少时间来写一个完美的问题.此外,这可能会出现在jQuery错误报告中,但我宁愿先在这里发布,因为我是一个相对的JavaScript新手,所以可能我做错了什么...
我创建了以下代码段来重现它.它使用PHP将接收到的数据 echo 到浏览器,尽管它可以在没有任何PHP的情况下工作.
The problem can be reproduced in Firefox 4 and Chrome 10. You'll need the console to see what's happening.
以下是代码:
<?
$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("更多信息:");
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>
When loaded, it throws some apparently unrelated parse error or exception (depending on the browser). The json that should be sent is {"something":"??"} but if you inspect it in the network tab of Firebug (or the Chrome equivalent) you'll see that "??" is being replaced by some jQuery string which looks like: jQuery152026845051744021475_1303152126170
And that is what the server receives.
This problem only happens when there are two or more consecutive question marks in the value field inside the sent JSON object, even if there are other letters in there. With one question mark it seems to work. Also changing "dataType" to "text" fixes this. But I need all these features!
如果您注释掉"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:17更多信息:
test.php:18jQuery15206220591682940722_1303153398797 was not called
test.php:19
Object
Firefox Firebug输出:
Now sending this: {"something":"??"} through Ajax...
Ajax Error: parsererror
更多信息:
jQuery15206494160738701454_1303153492631 was not called
Object { readyState=4, responseText="{"something":"jQuery152...8701454_1303153492631"}", more...}
invalid label
{"something":"jQuery15206494160738701454_1303153492631"}