The ajax function refers to the jQuery.ajax() function, which is used to load remote data through background HTTP requests. It is an AJAX technology implementation encapsulated by jQuery. Through this function, we can obtain remote data without refreshing the current page. data on the server.
The operating environment of this article: Windows 7 system, jquery version 3.2.1, Dell G3 computer.
What is the ajax function?
jQuery.ajax() function detailed explanation
jQuery.ajax()
function is used to load through background HTTP requests Remote data.
jQuery.ajax()
The function is an implementation of AJAX technology encapsulated by jQuery. Through this function, we can obtain data on the remote server without refreshing the current page.
jQuery.ajax()
The function is the underlying AJAX implementation of jQuery. jQuery.get(), jQuery.post(), load(), jQuery.getJSON(), jQuery.getScript() and other functions are all simplified forms of this function (they all call this function, but the parameter settings are different or have some differences). omitted).
This function belongs to the global jQuery
object (can also be understood as a static function).
Please find the corresponding parameters according to the parameter names defined in the previous syntax section.
Parameters | Description |
---|---|
url | String type URL request string. |
settings | Optional/Object type An Object object, each attribute of which is used to specify additional parameter settings required to send a request . |
Parameterssettings
is an object, jQuery.ajax()
can identify the following properties of the object (they are all optional):
Default value: Depends At dataType
attribute.
The content type request header sent is used to tell the server what type of response the browser can receive from the server.
Default value: true
.
Indicates whether it is an asynchronous request. Synchronous requests lock the browser until the remote data is obtained and no other operations can be performed.
Specify what needs to be executed before the request is sent Callback. This function also has two parameters: one is the jqXHR
object, and the other is the current settings
object. This is an Ajax event. If the function returns false
, this ajax request will be cancelled.
Default value: true
(When dataType
is 'script' or 'jsonp', the default is false
).
Indicates whether to cache URL requests. If set to false
it will force the browser not to cache the current URL request. This parameter is only valid for HEAD and GET requests (POST requests themselves will not be cached).
Specified requestComplete The callback function that needs to be executed after (regardless of success or failure). This function also has two parameters: one is the jqXHR
object, and the other is a string representing the request status ('success', 'notmodified', 'error', 'timeout', 'abort' or 'parsererror '). This is an Ajax event.
Starting from jQuery 1.5, the attribute value can be multiple functions in the form of array, and each function will be executed by a callback.
An object paired with "{string:regular expression}" to determine how jQuery will parse the response, given its content type.
Default value: 'application/x- www-form-urlencoded; charset=UTF-8'.
Send data to the server using the specified content encoding type. The W3C's XMLHttpRequest specification stipulates that the charset is always UTF-8. If you change it to another character set, you cannot force the browser to change the character encoding.
Used to set Ajax related callback functions Context object (that is, the this
pointer within the function).
Default value: {'* text': window.String, 'text html': true, 'text json': jQuery.parseJSON, 'text <span id="9_nwp">xml': jQuery.parseXML}</span>
.
A data type converter. The value of each converter is a function that returns the converted value of the response.
Default value: Same-domain request is false
, cross-domain request is true
.
Indicates whether it is a cross-domain request. If you want to force cross-domain requests within the same domain (as in JSONP form), set this to true. This allows server-side redirection to another domain, for example.
sent to the server will be automatically forwarded is of string type. If it is a GET request, it will be appended to the URL.
Specifies the callback for processing the raw data of the response function. This function also has two parameters: one is a string representing the original data of the response, and the other is the <span id="8_nwp">dataType</span>
attribute string.
Default value: jQuery smart guess, guess Scope (xml, json, script or html)
Specifies the data type returned. The attribute value can be:
cache
parameter is set. Note: When making remote requests (not under the same domain), all POST requests will be converted into GET requests. (Because the DOM script tag will be used to load)
null
or {}
.
Specify the callback function to be executed when the request fails. This function has 3 parameters: jqXHR object, request status string (null, 'timeout', 'error', 'abort' and 'parsererror'), error message string (text description part of the response status, such as 'Not Found' ' or 'Internal Server Error'). This is an Ajax event. Cross-domain scripts and cross-domain JSONP requests will not call this function.
Starting from jQuery 1.5, the attribute value can be multiple functions in the form of array , each function will be executed by a callback.
Default value: true
.
Indicates whether to trigger the global Ajax event. Setting this value to false
will prevent global event handlers from being triggered, such as ajaxStart() and ajaxStop(). It can be used to control various Ajax events.
default value:{}
.
Specify additional request header information in object form. The request header X-Requested-With: XMLHttpRequest
will always be added, but you can also modify the default XMLHttpRequest value here. The value in headers
can override the request header set in the beforeSend
callback function (meaning beforeSend is called first).
$.ajax({ url: "my.php" , headers: { "Referer": "http://www.365mini.com" // 有些浏览器不允许修改该请求头 ,"User-Agent": "newLine" // 有些浏览器不允许修改该请求头 ,"X-Power": "newLine" ,"Accept-Language": "en-US" } });
Default value: false
.
Allows the current request to obtain new data only when the server data changes (if it has not changed, the browser obtains the data from the cache). It uses HTTP header information Last-Modified
to determine. Starting with jQuery 1.4, it also checks the server-specified 'etag' to determine whether the data has been modified.
Default: Depends on the current location protocol.
允许将当前环境视作"本地",(例如文件系统),即使默认情况下jQuery不会如此识别它。目前,以下协议将被视作本地:file
、*-extension
和widget
。
重写JSONP请求的回调函数名称。该值用于替代"url?callback=?"中的"callback"部分。
为JSONP请求指定一个回调函数名。这个值将用来取代jQuery自动生成的随机函数名。
从jQuery 1.5开始,你也可以指定一个函数来返回所需的函数名称。
一个mime类型用来覆盖XHR的mime类型。
用于响应HTTP访问认证请求的密码。
默认值:true
。
默认情况下,通过<span id="4_nwp">data</span>
属性传递进来的数据,如果是一个对象(技术上讲,只要不是字符串),都会处理转化成一个查询字符串,以配合默认内容类型 "application/x-www-form-urlencoded"。如果要发送 DOM树信息或其它不希望转换的信息,请设置为false
。
设置该请求加载的脚本文件的字符集。只有当请求时dataType为"jsonp"或"script",并且type是"GET"才会用于强制修改charset。这相当于设置