Home Web Front-end JS Tutorial Compare the differences between success and complete in jQuery.ajax

Compare the differences between success and complete in jQuery.ajax

May 11, 2018 pm 04:03 PM
success


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

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 nameTypeDescription
url String(Default: current page address) The address to send the request.
typeString(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.
timeoutNumberSet 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 FunctionFunctions 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
         }
Copy after login
cache Boolean(Default: true) jQuery 1.2 new feature, set to false The request information will not be loaded from the browser cache.
complete FunctionAfter 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
         }
Copy after login
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, which 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
         }
Copy after login
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
         }
Copy after login

The above is the detailed content of Compare the differences between success and complete in jQuery.ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use success method in php How to use success method in php Mar 06, 2023 am 09:12 AM

The success method in php is used to display a success message. Its usage syntax is "Success('...','Http://www.xxx.com/Admin/User/Index');", when we do not write Url, the system will determine whether there is a previous page. If there is a system, it will jump to the previous page, otherwise it will not jump.

How to use the success method in laravel How to use the success method in laravel Aug 10, 2023 pm 02:09 PM

In Laravel, the success method is used to return a successful response in the controller. To use the success method, we need to introduce the Response class in the controller. Then, we can use an instance of this class to call the success method. You can set the response message, the data to be returned, the response status code, and pass these parameters to the success method. A successful response will be returned, which contains the message and user data we set.

Learn best practices for success methods in PHP Learn best practices for success methods in PHP Mar 22, 2024 pm 02:51 PM

To learn best practices for success methods in PHP, specific code examples are required. PHP is a popular server-side scripting language that is widely used in the field of web development. In PHP, the success method is a common method used to determine whether an operation is successful or not, and is usually used to return a success message or code. To learn the best practices for success methods in PHP, you need to demonstrate and explain them with actual code examples. First, let's look at a simple example showing a successful succes

Optimize project development: Master Laravel's success method Optimize project development: Master Laravel's success method Mar 10, 2024 pm 10:00 PM

Optimizing project development: Mastering Laravel's success method requires specific code examples. In today's fast-paced software development environment, how to develop projects efficiently and ensure the stable operation of projects has become an important challenge faced by developers. As one of the popular PHP development frameworks today, Laravel provides many convenient functions and methods to help developers quickly build and optimize projects. Among them, the success method is a very useful function in Laravel, which can be used during project development.

Efficient application skills of Laravel success method Efficient application skills of Laravel success method Mar 10, 2024 pm 01:30 PM

Efficient application skills of Laravelsuccess method Laravel is a very powerful PHP framework. It provides many convenient methods and tools to help developers quickly build high-quality web applications. Among them, the success method is a method often used in development and is very practical when processing successful returns. This article will introduce the efficient application techniques of the success method and provide specific code examples to help readers better understand and apply this method. How the success method works

Use Laravel success method to improve development efficiency Use Laravel success method to improve development efficiency Mar 11, 2024 pm 02:06 PM

Title: Using the LaravelSuccess method to improve development efficiency In Laravel development, successfully processing user requests and returning relevant information is a very important task. In order to simplify this process and improve development efficiency, Laravel provides the success method. This article will explore how to use the success method to simplify the processing of successful responses and provide specific code examples. Overview Laravel's success method is used to generate a success response and

In-depth understanding of the success method in Laravel In-depth understanding of the success method in Laravel Mar 09, 2024 pm 09:15 PM

When we develop applications using the Laravel framework, we often encounter situations where we need to display prompt information after a successful operation. In this case, we can use the success method in Laravel to achieve it quickly and easily. This article will delve into the use of the success method in Laravel, including its functions, parameter meanings, and specific code examples. 1. The function of the success method In the Laravel framework, the success method belongs to the Session

Guide to using success method in Laravel Guide to using success method in Laravel Mar 10, 2024 pm 03:45 PM

Guide to using the success method in Laravel In the Laravel framework, the success method is a very commonly used method for returning a successful response. It is especially common in API development. It can help us standardize the return data format and make front-end development more convenient. In this article, we will detail the usage guidelines of the success method and provide specific code examples to demonstrate its usage. What is success method? success method in Laravel framework

See all articles