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

前端 未结 3 1457
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 17:38

I have the following ajax request:

        jQuery.ajax({
            async: true,
            type: \"GET\",
            url: url,
            data: data,
           


        
3条回答
  •  再見小時候
    2020-12-20 18:18

    I do a lot of testing with file: urls instead of using a web server. My JSON code will always have the wrong MIME type. 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.

提交回复
热议问题