LCJ
LCJ

Reputation: 22661

jQuery Ajax Error object is undefined

I hvae a webmethod called using jquery ajax. I hits the error callback. Fine - I thought I will analyze the error - but it is coming as undefined.

What are the possibilities for the error values to be undefined? How to fix this, if it is a trivial error?

Note: xhr,status and error are undefined.

Note: I am using Chrome version 35 and IE 8

CODE

$(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()
    });
});

Upvotes: 2

Views: 12544

Answers (1)

jfriend00
jfriend00

Reputation: 708116

You need to pass a reference to the function so change this:

error: errorFunction()

to this:

error: errorFunction

When you put the parens there, you were actually calling the function immediately and passing it's return. Without the parens, it is just a reference to the function that can be called later by the jQuery ajax infrastructure.


To further understand what was happening, your code error: errorFunction() was calling errorFunction() immediately with no arguments (which is what you were seeing in your debugging) and then fetching the return value from that function (which is undefined) and then putting that into your data structure which was passed to the ajax call. So essentially, you were doing the equivalent of this:

$(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
    });
});

If you aren't using errorFunction() in other places, then a more common way to do this is to define it inline like you did with the success handler like this:

$(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);
        }
    });
});

Upvotes: 5

Related Questions