我希望这足够清楚,我已经花了很多时间试图解决这个问题,所以我可能没有多少时间来写一个完美的问题.此外,这可能会出现在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"}

推荐答案

当使用jsonp时,jQuery使用??作为回调函数的占位符.当它解析Ajax请求时,如果发现双重问号(或更多问号),它会自动假定您正在try 使用jsonp.当您手动设置内容类型时,它将忽略问号.

So, avoid the problem by using contentType:

$.ajax(
    url: "your-url.php",
    dataType: "json",                 // what you expect the server to return
    contentType: "application/json",  // what you are sending
    ...
);

For reference:

jQuery Bugtracker: $.AJAX CHANGED THE POST CONTENT IF INCLUDE "??" (2 QUESTION MARK)

Hopes it saves someone else from hours of debugging...

Json相关问答推荐

使用JSONata将具有相同键的字典合并

在Jenkins中使用ReadJSON读取json子元素

Bash和echo命令出现意外结果

Vegalite中分组条形图中的偏移量问题

NoneType 对象的 Python 类型错误

使用 jq 和脚本 bash 映射两个 json

如何使用 SQL Server 将 json 存储为字符串的列分解/规范化为行和列?

TSQL FOR JSON 嵌套值

Servicestack 返回数组而不是带有数组的对象

UTF-8 字符编码之战 json_encode()

Swift - 将图像从 URL 写入本地文件

使用 API 搜索维基百科

PostgreSQL 中的 JSON 模式验证?

jQuery JSON 响应总是触发 ParseError

JSON 使用 simplejson 序列化 Django 模型

Swift :将 struct 转换为 JSON?

有 Json 标签但未导出

强制 JSON.NET 在序列化 DateTime 时包含毫秒(即使 ms 组件为零)

如何在dart Flutter 中将json字符串转换为json对象?

Java HashMap 与 JSONObject