简体   繁体   English

当http状态代码为“200 OK”时,为什么$ .ajax调用json数据会触发错误回调?

[英]Why does $.ajax call for json data trigger the error callback when http status code is “200 OK”?

I have the following ajax request: 我有以下ajax请求:

        jQuery.ajax({
            async: true,
            type: "GET",
            url: url,
            data: data,
            dataType: "json",
            success: function(results){
                currentData = results;
            },
            error: function(xhr, ajaxOptions, thrownError){
                if (xhr.status == 200) {
                    console.debug("Error code 200");
                }
                else {
                    currentData = {};
                    displayAjaxError(xhr.status);
                }
            }
        });

For some reason the error callback is called event though the http status code is 200 ie. 由于某种原因,错误回调被称为事件,虽然http状态代码是200即。 the request is OK. 请求没问题。 Why is this? 为什么是这样?

The problem might be that the json data returned from the url is malformed. 问题可能是从url返回的json数据格式不正确。 When the server actually returns something, the http status code is 200. But that doesn't mean that the data is proper json. 当服务器实际返回某些内容时,http状态代码为200.但这并不意味着数据是正确的json。 Check that the stringified json data returned is correctly formed. 检查返回的字符串化json数据是否正确形成。

I'm answering my own guestion because I learned this the hard way. 我正在回答我自己的猜测,因为我很难学到这一点。 I hadn't escaped a "-quote character in my json data. This resulted in very odd behaviour. Luckily the double quote character is pretty much the only character that needs to be escaped from data delivered via JSON. (More on this issue: Where can I find a list of escape characters required for my JSON ajax return type? ) 我没有在我的json数据中转义“-quote”字符。这导致了非常奇怪的行为。幸运的是,双引号字符几乎是唯一需要通过JSON传递的数据转义的字符。(更多关于此问题: 我在哪里可以找到我的JSON ajax返回类型所需的转义字符列表?

Does your callback return a page with Content-type: application/json ? 你的回调是否返回Content-type: application/json If not, that could well be the reason. 如果没有,那很可能就是原因。

I do a lot of testing with file: urls instead of using a web server. 我使用file:urls进行了大量测试,而不是使用Web服务器。 My JSON code will always have the wrong MIME type. 我的JSON代码将始终具有错误的MIME类型。 To take care of this I use the following code: 为了解决这个问题,我使用以下代码:

$(document).ready(
    function (){

        myData = {};
        $.ajax({
            type: "GET",
            // url: "json.php?fn=jsonData.json",        // with Apache
            url: "jsonData.json",                       // As a file:/// URL
            contentType: "application/json; charset=utf-8",
            data: myData,
            beforeSend: function(x) {
                if(x && x.overrideMimeType) {
                    x.overrideMimeType("application/json; charset=UTF-8");
                }
            },
            dataType: "json",

            success: function(returnData){
                 $("#jsonData").html("Success:"+returnData.tag);
            },
            error: function(returnData) {
                 $("#jsonData").html("Error:"+returnData.tag);
            }
        });
    }
);

This will force the MIME type to be correct for JSON data. 这将强制MIME类型对于JSON数据是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM