jquery ajax调用使用readystate 4,状态200,statustext ok返回错误 [英] jquery ajax call returning an error with readystate 4, status 200, statustext ok

查看:1363
本文介绍了jquery ajax调用使用readystate 4,状态200,statustext ok返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真让我难以置信。我从ajax收到错误回调。但是,如果我从错误消息中获取res.responseText(返回正确,顺便说一句)并使用它,它就做对了。就好像我收到了成功回调一样。

This is really boggling my mind. I get an error callback from ajax. But if I take the res.responseText (which comes back correct, btw) from the error message and use it, it does the right thing. Just as if I had received a success callback.

数据设置如下:

var dataToSend = {fieldname : textdata};

和ajax调用是这样的:

and the ajax call is like this:

var ajaxOptions = {
    url: '/newpage',
    data: JSON.stringify(dataToSend),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    cache: false,
    processData: false,
    type: 'POST',
    success: function(res) {
        console.log("success!");
        $('#' + divname).html(res);
    },
    error: function(res) {
        console.log("There was an error: " + JSON.stringify(res));
        $('#' + divname).html(res.responseText);
    }
};

$.ajax(ajaxOptions);

错误消息是:出现错误: {readyState: 4,responseText[这部分完全正常],status:200,statusText:OK}

The error message is : There was an error: {"readyState":4,"responseText" [this part is perfectly fine], "status":200, "statusText":"OK"}.

推荐答案

如果您的responseText不是正确的JSON,则会引发解析错误。 确保您的回复是有效的JSON 删除 dataType:json

If your responseText isn't a correct JSON, a parsing error is thrown. Either make sure your response is a valid JSON or remove dataType: "json".

来自 jQuery docs


dataType (默认值:Intelligent Guess(xml,json,script或html))

dataType (default: Intelligent Guess (xml, json, script, or html))

类型:字符串

您期望从服务器返回的数据类型。如果没有指定
,jQuery将尝试根据
的MIME类型推断它(XML MIME类型将产生XML,在1.4 JSON中将产生
a JavaScript对象,在1.4中脚本将执行脚本,
其他任何东西都将作为字符串返回)。可用类型(以及
作为成功回调的第一个参数传递的结果)是:

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

...


json:将响应计算为JSON并返回JavaScript
对象。跨域json请求转换为jsonp,除非
请求在其请求选项中包含jsonp:false。 JSON
数据以严格的方式解析;任何格式错误的JSON都会被拒绝,
会抛出一个解析错误。从jQuery 1.9开始,一个空的响应也被
拒绝;服务器应该返回null或{}的响应。
(有关正确的JSON格式的更多信息,请参阅json.org。)

"json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

这篇关于jquery ajax调用使用readystate 4,状态200,statustext ok返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆