为什么 Ajax 请求成功,但最终以失败回调告终

why ajax request success, but it end up in fail call back

本文关键字:失败 回调 Ajax 请求 成功 为什么      更新时间:2023-09-26

我正在使用带有 ASP.net MVC的jQuery(我认为后端并不重要)

在 JavaScript 中,我有一个这样的辅助函数

postAsync: function (url, data) {
            return $.ajax({
                url: url,
                data: JSON.stringify(data),
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json'
            });
        }

当我想与服务器交互时,我会做

postAsync("/mycontroler/methodname", {input: value}).done(function(){
  // show success message
}).fail(function(err){
  // show error message
});

但是,我所有的 ajax 请求总是以"失败"回调告终。我查看"错误"对象,我可以看到

status: 200
statusText: "OK"

但是为什么它最终会出错。 我调试了我的控制器,它很好!!一点也不例外。

知道吗?

如果你的服务器没有给你json,状态实际上是200/OK,但jquery无法解析json,所以它失败了。或者它可能是一个空答案,被认为是无效的 json 内容,因为 jQuery 1.9 : http://jquery.com/upgrade-guide/1.9/#jquery-ajax-returning-a-json-result-of-an-empty-string – 锆 15 分钟前

返回空对象

解决问题