Specify only the second parameter in a javascript function - Stack Overflow

The spec for the jQuery ajax.error function is:error(XMLHttpRequest, textStatus, errorThrown)FunctionI&

The spec for the jQuery ajax.error function is:

error(XMLHttpRequest, textStatus, errorThrown)Function

I'm trying to catch the error and display the textStatus, but I can't figure out how to specify only the textStatus without having to put in a variable name for XMLHttpRequest and errorThrown. My code currently 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);
    }
});

You can see in my code that I'm putting variables a and b as placeholders for the first and last variables in the error function. I know that in my success function, I'm only providing one parameter and it works fine, but in that case data is the first parameter. In the case of error, textStatus is the second parameter, but that's the only one I want to specify. Is this possible?

The spec for the jQuery ajax.error function is:

error(XMLHttpRequest, textStatus, errorThrown)Function

I'm trying to catch the error and display the textStatus, but I can't figure out how to specify only the textStatus without having to put in a variable name for XMLHttpRequest and errorThrown. My code currently 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);
    }
});

You can see in my code that I'm putting variables a and b as placeholders for the first and last variables in the error function. I know that in my success function, I'm only providing one parameter and it works fine, but in that case data is the first parameter. In the case of error, textStatus is the second parameter, but that's the only one I want to specify. Is this possible?

Share Improve this question asked May 12, 2010 at 20:23 Ben McCormackBen McCormack 33.1k49 gold badges153 silver badges225 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

After programming in Erlang for a while, I started using "_" as the name of a variable that I don't care about. It's just a convention like anything else but it clarifies situations like that for me. Javascript seems to be OK with using "_" for more than one parameter, I think.

If you really want to, you can use the arguments array:

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

However, it's a horrible idea; you should simply add an unsused parameter.

No. The closest that you can get is:

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

Only the last parameters can be omitted

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742346856a4426717.html

相关推荐

  • Specify only the second parameter in a javascript function - Stack Overflow

    The spec for the jQuery ajax.error function is:error(XMLHttpRequest, textStatus, errorThrown)FunctionI&

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信