javascript - jQuery Ajax 错误对象未定义

标签 javascript jquery

我有一个使用 jquery ajax 调用的 webmethod。我点击了错误回调。很好 - 我以为我会分析错误 - 但它是未定义的。

错误值未定义的可能性有哪些?如果这是一个微不足道的错误,如何解决这个问题?

注意:xhrstatuserror 未定义。

注意:我使用的是 Chrome 版本 35 和 IE 8

代码

$(document).ready(function () {
    function errorFunction(xhr, status, error) {
        console.log(xhr);
        if (xhr == 'undefined' || xhr == undefined) {
            alert('undefined');
        } else {
            alert('object is there');
        }
        alert(status);
        alert(error);
    }

    $.ajax({
        type: "POST",
        url: "admPlantParametersViewEdit.aspx/GetResult",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert("success");
            alert(msg.d);
        },
        error: errorFunction()
    });
});

最佳答案

您需要传递对函数的引用,因此更改此:

error: errorFunction()

对此:

error: errorFunction

当你把括号放在那里时,你实际上是立即调用该函数并传递它的返回值。如果没有括号,它只是对稍后可由 jQuery ajax 基础结构调用的函数的引用。


为了进一步了解发生的情况,您的代码 error: errorFunction() 立即调用 errorFunction() 且不带任何参数(这就是您在调试中看到的情况) ),然后从该函数获取返回值(未定义),然后将其放入传递给 ajax 调用的数据结构中。所以本质上,你所做的相当于:

$(document).ready(function () {
    function errorFunction(xhr, status, error) {
        console.log(xhr);
        if (xhr == 'undefined' || xhr == undefined) {
            alert('undefined');
        } else {
            alert('object is there');
        }
        alert(status);
        alert(error);
    }

    // obviously, not what you intended
    errorFunction();

    $.ajax({
        type: "POST",
        url: "admPlantParametersViewEdit.aspx/GetResult",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert("success");
            alert(msg.d);
        },
        // also not what you intended
        error: undefined
    });
});

如果您没有在其他地方使用 errorFunction(),那么更常见的方法是像使用 success 处理程序一样内联定义它像这样:

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "admPlantParametersViewEdit.aspx/GetResult",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert("success");
            alert(msg.d);
        },
        error: function(xhr, status, error) {
            console.log(xhr);
            if (xhr == 'undefined' || xhr == undefined) {
                alert('undefined');
            } else {
                alert('object is there');
            }
            alert(status);
            alert(error);
        }
    });
});

关于javascript - jQuery Ajax 错误对象未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24174404/

相关文章:

javascript - 重新加载时 Python Flask 304 响应

javascript - Ajax 绑定(bind)事件后不起作用

javascript - 获取显示窗口的最后一个表格 tr 和 div

javascript - Typescript 如何从循环中调用和访问字典键

javascript - 有没有办法在前端 Web 应用程序中使用 Twitter API?

php - 如何使用uploadify提交表单后获取上传的文件?

javascript - 灯箱图片都是黑白的

javascript - jQuery ajax 请求代理

jquery - 这里哪个函数已被弃用?

javascript - jQuery 包装由 Div 中的 .Each 方法创建的元素