Home Web Front-end JS Tutorial Detailed explanation of various parameters of ajax in jquery

Detailed explanation of various parameters of ajax in jquery

Jun 19, 2017 am 10:12 AM
ajax jquery parameter Detailed explanation

ajax() parameter settings is an Object object, each attribute of which is used to specify additional parameter settings required to send the request.

jQuery.ajax() can recognize the following properties of the object (they are all optional):

1.url:

Requires parameters of type String, (Defaults to the current page address) The address to send the request.

2.type:

Requires parameters of String type, and the request method (post or get) defaults to get. Note that other http request methods, such as put and delete, can also be used, but are only supported by some browsers.

3.timeout:

Requires a parameter of type Number and sets the request timeout (milliseconds). This setting will override the global setting of the $.ajaxSetup() method.

4.async:

Requires Boolean type parameters. The default setting is true. 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.

5.cache:

Requires parameters of Boolean type, the default is true (when dataType is script, the default is false), setting it to false will not load from the browser cache Requesting Information.

6.data:

Requires parameters of type Object or String, data sent to the server. If it is not a string, it will be automatically converted to string format. The get request will be appended to the url. To prevent this automatic conversion, check the processData option. The object must be in key/value format, for example {foo1:"bar1",foo2:"bar2"} is converted to &foo1=bar1&foo2=bar2. 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.

7.dataType:

Requires parameters of String type, and expects the data type 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 . The available types are as follows:
xml: Returns an XML document that can be processed with JQuery.
html: Returns plain text HTML information; the included script tag will be executed when inserted into the DOM.
script: Returns plain text JavaScript code. Results are not cached automatically. Unless cache parameters are set. Note that when making remote requests (not under the same domain), all post requests will be converted into get requests.
json: Return JSON data.
jsonp: JSONP format. When calling a function using the SONP form, such as myurl?callback=?, JQuery will automatically replace the last "?" with the correct function name to execute the callback function.
text: Returns a plain text string.

8.beforeSend:

requires parameters of Function type. You can modify the function of the XMLHttpRequest object before sending the request, such as adding a custom HTTP header. If false is returned in beforeSend, this ajax request can be canceled. The XMLHttpRequest object is the only parameter.​​ ​

 function(XMLHttpRequest){
  this;   //调用本次ajax请求时传递的options参数
 }
Copy after login

9.complete:

is required to be a parameter of Function type, a callback function that is called after the request is completed (called when the request succeeds or fails). Parameters: XMLHttpRequest object and a string describing the successful request type.​​​​

 function(XMLHttpRequest, textStatus){
    this;    //调用本次ajax请求时传递的options参数
 }
Copy after login

10.success: The requirement is a parameter of Function type. The callback function called after the request is successful has two parameters.

         (1) Data returned by the server and processed according to the dataType parameter.
(2) Describe the string of the state.​​​​

function(data, textStatus){
   //data可能是xmlDoc、jsonObj、html、text等等
   this;  //调用本次ajax请求时传递的options参数
}
Copy after login

11.error:

is required to be a parameter of Function type, which is the function to be called when the request fails. This function has 3 parameters, namely XMLHttpRequest object, error message, and captured error object (optional). The ajax event function is as follows:

function(XMLHttpRequest, textStatus, errorThrown){
    //通常情况下textStatus和errorThrown只有其中一个包含信息
    this;   //调用本次ajax请求时传递的options参数
}
Copy after login

12.contentType:

is required to be a String type parameter. When sending information to the server, the content encoding type defaults to "application/x-www-form- urlencoded". This default value is suitable for most applications.

13.dataFilter:

Requires parameters of Function type, a function that preprocesses the original data returned by Ajax. Provide two parameters: data and type. data is the original data returned by Ajax, and type is the dataType parameter provided when calling jQuery.ajax. The value returned by the function will be further processed by jQuery.           

function(data, type){
    //返回处理后的数据
    return data;
}
Copy after login

14.dataFilter:

requires parameters of Function type, a function that preprocesses the original data returned by Ajax. Provide two parameters: data and type. data is the original data returned by Ajax, and type is the dataType parameter provided when calling jQuery.ajax. The value returned by the function will be further processed by jQuery.           

function(data, type){
   //返回处理后的数据
   return data;
}
Copy after login

15.global:

要求为Boolean类型的参数,默认为true。表示是否触发全局ajax事件。设置为false将不会触发全局ajax事件,ajaxStart或ajaxStop可用于控制各种ajax事件。

16.ifModified:

要求为Boolean类型的参数,默认为false。仅在服务器数据改变时获取新数据。服务器数据改变判断的依据是Last-Modified头信息。默认值是false,即忽略头信息。

17.jsonp:

要求为String类型的参数,在一个jsonp请求中重写回调函数的名字。该值用来替代在"callback=?"这种GET或POST请求中URL参数里的"callback"部分,例如{jsonp:'onJsonPLoad'}会导致将"onJsonPLoad=?"传给服务器。

18.username:

要求为String类型的参数,用于响应HTTP访问认证请求的用户名。

19.password:

要求为String类型的参数,用于响应HTTP访问认证请求的密码。

20.processData:

要求为Boolean类型的参数,默认为true。默认情况下,发送的数据将被转换为对象(从技术角度来讲并非字符串)以配合默认内容类型"application/x-www-form-urlencoded"。如果要发送DOM树信息或者其他不希望转换的信息,请设置为false。

21.scriptCharset:

要求为String类型的参数,只有当请求时dataType为"jsonp"或者"script",并且type是GET时才会用于强制修改字符集(charset)。通常在本地和远程的内容编码不同时使用。

22.jsonpCallback

为jsonp请求指定一个回调函数名。jquery会自动生成随机函数名,用这个值可以修改此名。

23. mimetype

jQuery1.5.1添加。可以用来覆盖XHR中的mimetype。

24. statusCode

jQuery1.5添加。用来定义http的返回码对应的处理函数。下面的例子定义了返回404后的处理方法。

$.ajax({   
    statusCode: {   
      404: function() {   
           alert("page not found");   
           }   
      }   
});
Copy after login

25.success(data,textStatus,jqXHR)

请求成功后的回调函数。参数由服务器返回,并会根据datatype参数进行参数处理。

26.timeout

设置请求超时时间,毫秒为单位。此设置会覆盖全局设置,即所有ajax请求共享同一个超时时间。

27.traditional

设置为true,用传统的方式来序列化数据。

28.type

请求方式,get或post或put或delete。默认为get。put和delte不是得到所有的浏览器支持。

29.url

发送请求的地址。为空表示当前页。

30.username

用于响应http访问认证请求的用户名。同password配对。

31.xhr

默认在ie下是ActiveXObject而其他浏览器是XMLHttpRequest。用于重写或提供一个增强的XMLHttpRequest对象。

32.xhrFields

jQuery1.5.1添加。它可以添加到原生xhr对象上的key/value对。举个例子,你可以通过它来设置跨域的withCredentials为true。

$.ajax({   
    url: a_cross_domain_url,   
    xhrFields: {   
    withCredentials: true   
    }   
});
Copy after login

The above is the detailed content of Detailed explanation of various parameters of ajax in jquery. 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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

C++ function parameter type safety check C++ function parameter type safety check Apr 19, 2024 pm 12:00 PM

C++ parameter type safety checking ensures that functions only accept values ​​of expected types through compile-time checks, run-time checks, and static assertions, preventing unexpected behavior and program crashes: Compile-time type checking: The compiler checks type compatibility. Runtime type checking: Use dynamic_cast to check type compatibility, and throw an exception if there is no match. Static assertion: Assert type conditions at compile time.

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Advanced usage of reference parameters and pointer parameters in C++ functions Advanced usage of reference parameters and pointer parameters in C++ functions Apr 21, 2024 am 09:39 AM

Reference parameters in C++ functions (essentially variable aliases, modifying the reference modifies the original variable) and pointer parameters (storing the memory address of the original variable, modifying the variable by dereferencing the pointer) have different usages when passing and modifying variables. Reference parameters are often used to modify original variables (especially large structures) to avoid copy overhead when passed to constructors or assignment operators. Pointer parameters are used to flexibly point to memory locations, implement dynamic data structures, or pass null pointers to represent optional parameters.

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

See all articles