2

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()
    });
});
3
  • The question is too vague. What exactly is undefined? Commented Jun 11, 2014 at 23:51
  • @zerkm s - xhr,status and error are undefined. I am using Chrome version 35 and IE 8 Commented Jun 11, 2014 at 23:53
  • Putting () after a function reference always calls the function. There is not magic involved here. You didn't put () after the success function either. Commented Jun 12, 2014 at 0:05

1 Answer 1

5

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);
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.