简体   繁体   English

jQuery ajax请求在控制台中成功,但未调用回调

[英]jQuery ajax request succeeding in console but callback isn't called

I have a jQuery POST request that looks something like this: 我有一个看起来像这样的jQuery POST请求:

$.ajax("/myurl",{
    data:{
        ...
    },
    mimeType:"application/json",
    dataType:"application/json",
    method:"POST",
    success:function(r){
        console.log("success");
    },
    error:function(r){
        console.log("error");
        console.log(r);
    }
});

I turned on XHR logging in my chrome console, and it says the XHR finished loading: POST http://localhost/myurl . 我在chrome控制台中打开了XHR日志记录,它说XHR finished loading: POST http://localhost/myurl However, "error" is logged in my console as well, along with an XHR object: 但是,“错误”以及XHR对象也记录在我的控制台中:

error
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
XHR finished loading: POST "http://localhost/myurl"

Why is it saying that the XHR finished loading, but at the same time there's an error? 为什么说XHR已完成加载,但同时出现错误? Why is there even an error? 为什么还会出现错误? On the server-side, the server receives the request and sends a response. 在服务器端,服务器接收请求并发送响应。

The most likely explanation is that what the server is sending back isn't valid JSON, so although the ajax succeeds, parsing the JSON fails. 最可能的解释是服务器发回的内容不是有效的JSON,因此尽管ajax成功,但解析JSON失败。

Your dataType is off, though, it should have one of these values: "xml" , "json" , "script" , or "html" — in your case, "json" . 但是,您的dataType处于关闭状态,它应该具有以下值之一: "xml""json""script""html" -在您的情况下为"json" You also don't need (and probably don't want) to set mimeType . 您也不需要(也可能不需要)设置mimeType

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

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