Javascript jQuery Ajax错误对象未定义

Javascript jQuery Ajax错误对象未定义,javascript,jquery,Javascript,Jquery,我使用jQueryAjax创建了一个称为webmethod的方法。我点击错误回调。很好-我想我会分析错误-但它是未定义的 错误值未定义的可能性有哪些?如果这是一个小错误,如何修复 注:xhr,状态和错误未定义 注意:我使用的是Chrome版本35和IE 8 代码 $(document).ready(function () { function errorFunction(xhr, status, error) { console.log(xhr); if

我使用jQueryAjax创建了一个称为webmethod的方法。我点击错误回调。很好-我想我会分析错误-但它是未定义的

错误值未定义的可能性有哪些?如果这是一个小错误,如何修复

注:
xhr
状态
错误
未定义

注意:我使用的是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
当你把paren放在那里时,你实际上是在立即调用函数并传递它的返回。如果没有paren,它只是对jqueryajax基础设施稍后可以调用的函数的引用


为了进一步了解发生了什么,您的代码
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);
        }
    });
});

这个问题太模糊了。什么是
未定义的
?@zerkm s-
xhr
状态
错误
未定义。我使用的是Chrome版本35和IE 8Puting
()
,函数引用后总是调用函数。这里没有魔法。您也没有将
()
放在
成功
函数之后。