Home  >  Article  >  Web Front-end  >  Detailed explanation of error function and parameters in jquery using ajax

Detailed explanation of error function and parameters in jquery using ajax

黄舟
黄舟Original
2017-06-27 11:24:451318browse

When using jquery's ajax method to send a request to the server, it is often necessary to use the error function to process error information. This article explains in detail the usage of the error function and each parameter in the function in ajax.

Generally, there are three parameters returned by the error function: function(jqXHR jqXHR, String textStatus, String errorThrown). Common calling codes are as follows:

  $.ajax({
        url: '/Home/AjaxGetData',            
        success: function (data) {
            alert(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {            /*错误信息处理*/
        }
    });

Here are detailed descriptions of these three parameters.

The first parameter jqXHR jqXHR: jqXHR here is a jqXHR object. Before Jquery1.4 and 1.4 versions, the XMLHttpRequest object was returned. After version 1.5, the jqXHR object was used. This object is a superset, that is, the object not only includes the XMLHttpRequest object, but also contains other more detailed attributes and information.

There are mainly 4 attributes here:

readyState: current state, 0-not initialized, 1-loading, 2-already loaded, 3-data interaction, 4-completed .

status: The returned HTTP status code, such as common 404, 500 and other error codes.

statusText: Error message corresponding to the status code, such as 404 error message is not found, 500 is Internal Server Error.

responseText: The text information returned by the server response

The second parameter String textStatus: The returned is the string type, indicating the returned status, different errors according to the server The following information may be returned: "timeout" (timeout), "error" (error), "abort" (abort), "parsererror" (parser error), and may also return a null value.

The third parameter String errorThrown: is also a string type, indicating the error message returned by the server. If an HTTP error is generated, the returned message is the error message corresponding to the HTTP status code, such as 404. Not Found, 500 Error Internal Server Error.

Sample code:

   $.ajax({
        url: '/AJAX请求的URL',            
        success: function (data) {
            alert(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {            /*弹出jqXHR对象的信息*/
            alert(jqXHR.responseText);
            alert(jqXHR.status);
            alert(jqXHR.readyState);
            alert(jqXHR.statusText);            /*弹出其他两个参数的信息*/
            alert(textStatus);
            alert(errorThrown);
        }
    });

The above is the detailed content of Detailed explanation of error function and parameters in jquery using ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn