Home >Web Front-end >JS Tutorial >Detailed description of jquery ajax error function and parameters
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: The jqXHR here is a jqXHR object. Before Jquery1.4 and 1.4 versions, the XMLHttpRequest object was returned. It will be used after version 1.5. The jqXHR 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 Interact, 4-Done.
status: 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 It is a string type, indicating the returned status. Depending on the error of the server, the following information may be returned: "timeout" (timeout), "error" (error), "abort" (abort), "parsererror" (parser error), It is also possible to return a null value.
The third parameter String errorThrown: It is also a string type, indicating the error message returned by the server. If an HTTP error is generated, the returned information is the HTTP status. The error message corresponding to the code, such as 404 Not Found, 500 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 description of jquery ajax error function and parameters. For more information, please follow other related articles on the PHP Chinese website!