Home  >  Article  >  Web Front-end  >  The difference between success and complete in jQuery.ajax

The difference between success and complete in jQuery.ajax

巴扎黑
巴扎黑Original
2018-05-14 15:05:301805browse

$.ajax({
      type: "post",
      url: url,
      dataType:'html',
      success: function(data) { },
     complete: function(XMLHttpRequest, textStatus) { },
     error: function(){}
});

success: Function called when the request is successful. This function will get one parameter: the data returned from the server. The function is called when the request is successful, i.e. status==200.
complete: Function called when the request is completed. This function will get two parameters: XMLHttpRequest object and a string describing the type of request success. The function is called when the request is completed, i.e. status==404, 403, 302....

So, when writing the success or complete method, pay attention to the parameters passed in, and use the parameter objects passed in to solve our problems

Parameter list:

Parameter name Type Description
url String (Default: current page address) The address to send the request.
type String (Default: "GET") Request method ("POST" or "GET") , the default is "GET". Note: Other HTTP request methods, such as PUT and DELETE can also be used, but are only supported by some browsers.
timeout Number Set the request timeout (milliseconds). This setting overrides the global setting.
async Boolean (Default: true) By default, all requests are asynchronous requests. If you need to send synchronous requests, set this option to false. Note that a synchronous request will lock the browser, and the user must wait for the request to complete before other operations can be performed.
beforeSend Function Functions that can modify the XMLHttpRequest object before sending the request, such as adding custom HTTP headers. The XMLHttpRequest object is the only parameter.
function (XMLHttpRequest) {         this; // the options for this ajax request
         }
cache Boolean (Default: true) jQuery 1.2 new feature, set to false The request information will not be loaded from the browser cache.
complete Function After the request is completedCallback function (both when the request succeeds or fails transfer). Parameters: XMLHttpRequest object, success information string.
function (XMLHttpRequest, textStatus) {         this; // the options for this ajax request
         }
contentType String (Default: "application/x-www-form-urlencoded" ) Content encoding type when sending information to the server. The default value is suitable for most applications.
data Object,
String
Data sent to the server. Will be automatically converted to request string format. Appended to the URL in GET requests. See the processData option description to disable this automatic conversion. Must be in Key/Value format. If it is an array, jQuery will automatically assign the same name to different values. For example, {foo:["bar1", "bar2"]} is converted to '&foo=bar1&foo=bar2'.
dataType String

The data type expected to be returned by the server. If not specified, jQuery will automatically return responseXML or responseText based on the HTTP package MIME information and pass it as the callback Function parameter. Available values:

"xml": Returns an XML document that can be processed by jQuery .

"html": Returns plain text HTML information; contains script elements.

"script": Returns plain text JavaScript code. Results are not cached automatically.

"json": Returns JSON data.

"jsonp": JSONP format. use When calling a function in JSONP format, such as "myurl?callback=?" jQuery will automatically replace ? with the correct function name to execute the callback function.

error Function (默认: 自动判断 (xml 或 html)) 请求失败时将调用此方法。这个方法有三个参数:XMLHttpRequest 对象,错误信息,(可能)捕获的错误对象。
function (XMLHttpRequest, textStatus, errorThrown) {         // 通常情况下textStatus和errorThown只有其中一个有值 
         this; // the options for this ajax request
         }
global Boolean (默认: true) 是否触发全局 AJAX 事件。设置为 false 将不会触发全局 AJAX 事件,如 ajaxStart 或 ajaxStop 。可用于控制不同的Ajax事件
ifModified Boolean (默认: false) 仅在服务器数据改变时获取新数据。使用 HTTP 包 Last-Modified 头信息判断。
processData Boolean (默认: true) 默认情况下,发送的数据将被转换为对象(技术上讲并非字符串) 以配合默认内容类型 "application/x-www-form-urlencoded"。如果要发送 DOM 树信息或其它不希望转换的信息,请设置为 false。
success Function 请求成功后回调函数。这个方法有两个参数:服务器返回数据,返回状态
function (data, textStatus) {         // data could be xmlDoc, jsonObj, html, text, etc...
         this; // the options for this ajax request
         }

The above is the detailed content of The difference between success and complete in jQuery.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