Javascript jquery ajax 调用返回错误,就绪状态为 4,状态为 200,状态文本正常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32637677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:00:43  来源:igfitidea点击:

jquery ajax call returning an error with readystate 4, status 200, statustext ok

javascriptjqueryajax

提问by lynvie

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.

这真是让我摸不着头脑。我从 ajax 收到错误回调。但是,如果我从错误消息中获取 res.responseText (返回正确,顺便说一句)并使用它,它会做正确的事情。就像我收到了成功回调一样。

The data is set like this:

数据是这样设置的:

var dataToSend = {fieldname : textdata};

and the ajax call is like this:

和 ajax 调用是这样的:

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);

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

错误消息是:有一个错误:{"readyState":4,"responseText" [this part is perfectly fine], "status":200, "statusText":"OK"}

回答by Diego

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

如果您的 responseText 不是正确的 JSON,则会引发解析错误。无论是确保你的反应是一个有效的JSON删除dataType: "json"

From jQuery docs:

来自jQuery 文档

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

Type: String

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:

数据类型(默认:智能猜测(xml、json、脚本或 html))

类型:字符串

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

...

...

"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.)

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

回答by Jai

This happens to be when you have dataTypeset to get the response but the response is not what you set in the datatype.

这恰好是当您dataType设置为获取响应但响应不是您在数据类型中设置的内容时。

So in your case dataType: 'json',is set and as you mentioned at the comment section you have set stringat the backend, so you need to change your datatype to text.

因此,在您的情况下dataType: 'json',已设置,并且正如您在评论部分中提到的那样,您已string在后端设置,因此您需要将数据类型更改为text.

change your datatype to:

将您的数据类型更改为:

dataType: 'text',

回答by Kai Noack

One way to solve the problem serverside is to echo an empty array, json encoded:

解决服务器端问题的一种方法是回显一个空数组,json 编码:

echo json_encode([]);
return;

Then the success function gets triggered instead of the error.

然后成功函数被触发而不是错误。

Or just change scriptside dataType: 'json',to dataType: 'text',to tell jquery to receive a text response.

或者只是改变scriptsidedataType: 'json',dataType: 'text',告诉jQuery来接收文本响应。