Home  >  Article  >  Web Front-end  >  Detailed introduction to using jquery ajax method and each parameter

Detailed introduction to using jquery ajax method and each parameter

高洛峰
高洛峰Original
2017-03-19 11:57:481140browse

jquery Detailed explanation of the ajax method and each parameter

1.$.ajax() has only one parameter: parameterkey/value Object, including each configuration and callback function information.

Parameter list:

##Boolean(Default: true) jQuery 1.2 New Function, setting it to false will not load the request information from the browser FunctionCallback function after the request is completed (called when the request succeeds or fails). Parameters: XMLHttpRequest object, success information String
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 The functions of the XMLHttpRequest object can be modified before sending the request, such as Add custom HTTP headers. The XMLHttpRequest object is the only parameter.
function (XMLHttpRequest) {

         this; // the options for this ajax request
         }
cache cache .
complete String.
function (XMLHttpRequest, textStatus) {

         this; // the options for this ajax request
         }

contentType (Default: "application/x-www-form -urlencoded") Content encoding type when sending information to the server. The default value is suitable for most applications. Tell the server the format of the data submitted from the browser.

For example: If we use the method JSON.stringify(obj) in

JSON2.js when submitting data, after formatting it into a json string, an error will be reported when submitting by default. At this time, you need to specify the submitted content format as: "application/json".

data Object,String
Send data to the server.

If the data

data type is JavaScript object or array, Jquery automatically calls the JQuery.param() method to send the The data is encoded into data in the "application/x-www-form-urlencoded" format (ie name=value&name1=value1); the JavaScript object must be in Key/Value format; if it is an array, jQuery will automatically correspond to the same name for different values. For example, {foo:["bar1", "bar2"]} is converted to '&foo=bar1&foo=bar2';

If the data data type is String type, it will directly default to the data according to "application/x- www-form-urlencoded" format encoding is completed and no longer converted.

The processData option can control whether to perform conversion. This option defaults to true.

dataType String

预期服务器返回的数据类型。设定HttpHeader中“Accept”域的内容,告诉服务器浏览器可以想要返回的数据格式类型,同时JQuery也会根据该类型对返回的数据进行处理。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息返回 responseXML 或 responseText,并作为回调函数参数传递,可用值:

"xml": 返回 XML 文档,可用 jQuery 处理。

"html": 返回纯文本 HTML 信息;包含 script 元素。

"script": 返回纯文本 JavaScript 代码。不会自动缓存结果。

"json": 返回 JSON 数据 。JQuery将返回的字符串格式数据自动转化为Javascript对象,便于直接使用obj.property格式访问。若没有指定该选项,即使返回的是JSON格式的字符串,JQuery也不会自动转换。

"jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。

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
         }

2. jQuery.get(url, [data], [callback], [type]): Use GET method to make asynchronous requests.

Parameters:
url (String): The URL address to send the request.
data (Map): (Optional) The data to be sent to the server, expressed in the form of Key/value pairs.
callback (Function): (optional) Callback function when loading is successful (this method is called only when the return status of Response is success).
type (String): (Optional) The official description is: Type of data to be sent. In fact, it should be the type requested by the client (JSON, XML, etc.)

3. jQuery.post( url, [data], [callback], [type] ): Use POST method to make asynchronous requests

Parameters:
url (String): URL address to send the request.
data (Map): (Optional) Data to be sent to the server, with Key/ Value is represented in the form of a key-value pair.
callback (Function): (optional) Callback function when loading is successful (this method is called only when the return status of Response is success).
type (String): (Optional) The official description is: Type of data to be sent. In fact, it should be the type requested by the client (JSON, XML, etc.)

1.$.ajax() has only one parameter: parameter key/value object, including each configuration and callback function information.

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 synchronous requests will lock the browser, and the user must wait for the request to complete before other operations can be performed.
beforeSend Function The functions of the XMLHttpRequest object can be modified 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 Request information will not be loaded from the browser cache.
complete Function Callback function after the request is completed (called when the request succeeds or fails). 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. Tell the server the format of the data submitted from the browser.

For example: If we use the method JSON.stringify(obj) in JSON2.js when submitting data, after formatting it into a json string, an error will be reported when submitting by default. At this time, you need to specify the submitted content format as: "application/json".

data Object,
String

Data sent to the server.

If the data data type is a JavaScript object or array, Jquery automatically calls the JQuery.param() method to encode the data to be sent into "application/x-www-form-urlencoded" format data before submission ( That is, name=value&name1=value1); the JavaScript object must be in Key/Value format; if it is an array, jQuery will automatically correspond to the same name for different values. For example, {foo:["bar1", "bar2"]} is converted to '&foo=bar1&foo=bar2';

If the data data type is String type, it will directly default to the data according to "application/x- www-form-urlencoded" format encoding is completed and no longer converted.

The processData option can control whether to perform conversion. This option defaults to true.

dataType String

预期服务器返回的数据类型。设定HttpHeader中“Accept”域的内容,告诉服务器浏览器可以想要返回的数据格式类型,同时JQuery也会根据该类型对返回的数据进行处理。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息返回 responseXML 或 responseText,并作为回调函数参数传递,可用值:

"xml": 返回 XML 文档,可用 jQuery 处理。

"html": 返回纯文本 HTML 信息;包含 script 元素。

"script": 返回纯文本 JavaScript 代码。不会自动缓存结果。

"json": 返回 JSON 数据 。JQuery将返回的字符串格式数据自动转化为Javascript对象,便于直接使用obj.property格式访问。若没有指定该选项,即使返回的是JSON格式的字符串,JQuery也不会自动转换。

"jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。

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
         }

2. jQuery.get(url, [data], [callback], [type]):使用GET方式来进行异步请求.

参数:
url (String) : 发送请求的URL地址.
data (Map) : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示。
callback (Function) : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。
type (String) : (可选)官方的说明是:Type of data to be sent。其实应该为客户端请求的类型(JSON,XML,等等)

3. jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求

参数:
url (String) : 发送请求的URL地址.
data (Map) : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示。
callback (Function) : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。
type (String) : (可选)官方的说明是:Type of data to be sent。其实应该为客户端请求的类型(JSON,XML,等等)

The above is the detailed content of Detailed introduction to using jquery ajax method and each parameter. 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