Provide only second parameter in javascript function

Specification for jQuery function ajax.error

:

error(XMLHttpRequest, textStatus, errorThrown)Function

      

I'm trying to catch the error and display textStatus

, but I can't figure out how to specify only textStatus

without specifying the variable name for XMLHttpRequest

and errorThrown

. Currently my code looks like this:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: hbAddressValidation.webServiceUrl,
    data: this.jsonRequest,
    dataType: "json",
    timeout: 5,
    success: function (msgd) {
         //...
    },
    error: function (a,textStatus,b) {
        $("#txtAjaxError").val("There was an error in the AJAX call: " 
                                + textStatus);
    }
});

      

In my code, you can see that I am putting variables a

and b

as placeholders for the first and last variables in the function error

. I know that in my success function I only provide one parameter and it works fine, but in this case it data

is the first parameter. In the case error

, textStatus

- the second parameter, but the only one I want to specify. Is it possible?

+2


a source to share


3 answers


No. The closest you can get:

error: function (a,textStatus) {
    $("#txtAjaxError").val("There was an error in the AJAX call: " 
                            + textStatus);
}

      



Only the last parameters can be omitted

+1


a source


After programming in Erlang for a while, I started using "_" as a variable name, which I don't care about. It's just a convention like everything else, but it clarifies situations like this for me. I believe Javascript works with "_" for more than one parameter.



+4


a source


If you really want it, you can use an array arguments

:

error: function () {
    var textStatus = arguments[1];    //Zero-based
    $("#txtAjaxError").val("There was an error in the AJAX call: " 
                            + textStatus);
}

      

However, this is a terrible idea; you should just add the unused parameter.

+2


a source







All Articles