In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail the "methods used in jQuery.ajax", with detailed content, clear steps and proper handling of details. I hope this article "methods used in jQuery.ajax" can help you solve your doubts.
JQuery.ajax (url [, settings]) instructions: execute an asynchronous HTTP (Ajax) request.
Url
Type: String
A URL string that contains the sending request.
Settings
Type: PlainObject
An AJAX request setting consisting of "{key: value}". All options are optional. You can use $.ajaxSetup () to set any default parameters. See the complete list of all settings under jQuery.ajax (settings).
JQuery.ajax ([settings])
Settings
Type: PlainObject
An AJAX request setting consisting of "{key: value}". All options are optional. You can use $.ajaxSetup () to set any default parameters.
Accepts (default: depending on data type)
Type: PlainObject
The content type send request header (Content-Type) is used to inform the server what type of return result the request needs to receive. If the accepts setting needs to be modified, it is recommended to set it once in the $.ajaxSetup () method.
Async (default: true)
Type: Boolean
By default, all requests are asynchronous (that is, this is set to true by default). If you need to send a synchronization request, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operations. Note that the synchronization request will lock the browser, and other user actions must wait for the request to complete. Since jQuery 1.8, the use of async: false in jqXHR ($.Deferred) has been out of date. You must use the callback option of success/error/complete instead of the method of the corresponding jqXHR object, such as jqXHR.done () or outdated jqXHR.success ()
BeforeSend
Type: Function (jqXHR jqXHR, PlainObject settings)
The callback function before the request is sent, which is used to modify the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object before the request is sent, this function is used to set custom HTTP header information, and so on. The jqXHR and the setting object are passed as parameters. This is an Ajax event. Returning false in the beforeSend function cancels the request. Starting with jQuery 1.5, the beforeSend option will be accessed, regardless of the type of request.
Cache (default: true, default is false when dataType is "script" and "jsonp")
Type: Boolean
If set to false, the browser will not cache this page. Note: setting cache to false will work correctly in HEAD and GET requests. It works by appending "_ = {timestamp}" to the GET request parameters. This parameter is not required for other requests, except in IE8, when a POST requests a URL that has already been requested with GET.
Complete
Type: Function (jqXHR jqXHR, String textStatus)
Callback function after the request is completed (both success and error are called after the request). This callback function takes two parameters: the jqXHR (XMLHTTPRequest in jQuery 1.4.x) object and a string that describes the status of the request ("success", "notmodified", "error", "timeout", "abort", or "parsererror"). Starting with jQuery 1.5, the complete setting can accept an array of functions. Each function will be called in turn. This is an Ajax event.
Contents
Type: PlainObject
An object paired with "{string / regular expression}" parses the returned result of the request according to the given content type. (1.5 New)
ContentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')
Type: String
The content encoding type when sending information to the server. The default value is "application/x-www-form-urlencoded; charset=UTF-8", which is suitable for most situations. If you explicitly pass a content type (Content-Type) to $.ajax (), it must be sent to the server (even if there is no data to send). Data will always be passed to the server using the UTF-8 character set; you must decode it properly on the server side.
Context
Type: Object
This object is used to set the context of the Ajax-related callback function. By default, this context is a parameter setting object used by the ajax request ($.ajaxSettings merges this setting and passes it to $.ajax). For example, specify a DOM element as the context parameter, which sets the context of the complete callback function to this DOM element. It's like this:
$.ajax ({
Url: "test.html"
Context: document.body
}) .done (function () {
$(this) .addClass ("done")
})
Converters (default: {"* text": window.String, "text html": true, "text json": jQuery.parseJSON, "text xml": jQuery.parseXML})
Type: PlainObject
An object that converts a data type to a data type. The value of each converter is a function that returns the converted request result. (1.5 New)
CrossDomain (default: same domain request is false, cross-domain request is true)
Type: Boolean
If you want to force cross-domain requests (such as JSONP) in the same domain, for example, if you want to redirect to another domain on the server side, you need to set crossDomain to true. (1.5 New)
Data
Type: Object, String
Data sent to the server. Will be automatically converted to the request string format. The GET request will be appended to the URL. Check the description of the processData option to disable this automatic conversion. Object must be in the format "{key: value}". If this parameter is an array, jQuery will automatically convert it to a multi-valued query string with the same name according to the value of the traditional parameter (see the description below). Note: for example, {foo: ["bar1", "bar2"]} is converted to'& foo=bar1&foo=bar2'.
DataFilter
Type: Function (Object data, String type) = > Object
A function is used to process the raw response data of the XMLHttpRequest. This is a pre-filtering function that purifies the response. You should return security data. Provide two parameters, data and type: data is the raw data returned by Ajax, and type is the dataType parameter provided when jQuery.ajax is called.
DataType (default: Intelligent Guess (xml, json, script, or html))
Type: String
The type of data expected to be returned by the server. If not specified, jQuery automatically determines intelligently based on the MIME information of the HTTP package, such as the XML MIME type is recognized as XML. In 1. 4, JSON generates a JavaScript object, and script executes the script. The data returned by the server is then parsed according to this value and passed to the callback function. Available values:
"xml": returns the XML document, which can be processed with jQuery. .
"html": returns plain text HTML information; the included script tag is executed when the dom is inserted.
"script": executes the result of the response as a JavaScript. And return it as plain text. By default, results are not automatically cached by appending the query string variable "_ = [TIMESTAMP]" to URL, unless the cache parameter is set to true. Note: when a remote request is made (not under the same domain), all POST requests are converted to GET requests. (because the script tag of DOM will be used to load)
"json": executes the result of the response as JSON and returns a JavaScript object. In jQuery 1.4, data in JSON format is parsed in a strict manner, and if there is an error in the format, jQuery is rejected and a parsed error exception is thrown. (see json.org for more information on the correct JSON format. )
"jsonp": loads JSON blocks as JSONP. "? callback=?" is automatically added at the end of the requested URL. By default, results are not automatically cached by appending the query string variable "_ = [TIMESTAMP]" to URL unless the cache parameter is set to true.
"text": returns a plain text string.
Multiple space split values: starting with jQuery 1.5, jQuery can receive and convert a data type you need from the content type (Content-Type) header. For example, if you want a text response for XML processing, use the "text xml" data type. You can also accept a JSONP request as text and parse it as XML with jQuery: "jsonp text xml". Similarly, you can use the abbreviation "jsonp xml". You will first try to convert from jsonp to xml, and if the conversion fails, convert jsonp to text first, and then from text to xml.
Error
Type: Function (jqXHR jqXHR, String textStatus, String errorThrown)
This function is called when the request fails. There are three parameters: the jqXHR (XMLHttpRequest before jQuery 1.4.x) object, a string that describes the type of error that occurred, and the exception object caught. If an error occurs, the error message (the second parameter) may be "timeout", "error", "abort", and "parsererror" in addition to getting null. When a HTTP error occurs, the errorThrown receives the text portion of the HTTP status, such as "Not Found" (not found) or "Internal Server Error." (server internal error). Starting with jQuery 1.5, an array of acceptable functions is set in error. Each function will be called in turn. Note: this handler is not called on requests in the form of cross-domain scripts and JSONP. This is an Ajax Event.
Global (default: true)
Type: Boolean
In any case, this request will trigger the global AJAX event handler. The default is true. Setting to false will not trigger global AJAX events, such as ajaxStart or ajaxStop. This can be used to control all kinds of Ajax Event.
Headers (default: {})
Type: PlainObject
An additional "{key: value}" pair is sent together with the mapping to the request. This setting is set before the beforeSend function is called; therefore, the setting value in the request header is overridden by the setting in the beforeSend function. (1.5 New)
IfModified (default: false)
Type: Boolean
The request is allowed to succeed only when the request response changes last time. Use HTTP package Last-Modified header information to determine. The default value is false and the HTTP header information is ignored. In jQuery 1.4, he also checks the server-specified 'etag'' to make sure that the data has not been modified.
IsLocal (default: depending on the current location protocol)
Type: Boolean
Allows the current environment to be recognized as "local" (such as a file system), even if jQuery does not do so by default. The following protocols are currently recognized as local: file, *-extension, and widget. If the isLocal setting needs to be modified, it is recommended to do so once in the $.ajaxSetup () method. (1.5 New)
Jsonp
Type: String
Rewrite the name of the callback function in an jsonp request. This value is used instead of in "callback=?" The "callback" part of the URL parameter in such a GET or POST request, such as {jsonp:'onJsonPLoad'}, causes the "onJsonPLoad=?" Pass it to the server. In jQuery 1.5, set the jsonp option to false to prevent jQuery from adding the "? callback" string to URL or trying to use "=?" Conversion. In this case, you should also explicitly set the jsonpCallback setting. For example, {jsonp: false, jsonpCallback: "callbackName"}
JsonpCallback
Type: String, Function
Specify a callback function name for the jsonp request. This value will be used to replace the random function name automatically generated by jQuery. This is mainly used to get jQuery to generate a unique function name, which makes it easier to manage requests and to provide callback functions and error handling. You can also specify this callback function name when you want the browser to cache GET requests. Starting with jQuery 1.5, you can also use a function as this parameter, in which case the return value of this function is the result of jsonpCallback.
MimeType
Type: String
A mime type is used to override the MIME type of XHR. (1.5 New)
Password
Type: String
Password used to respond to HTTP access authentication requests
ProcessData (default: true)
Type: Boolean
By default, data passed in through the data option, if it is an object (technically, as long as it is not a string), is processed and converted into a query string to match the default content type "application/x-www-form-urlencoded". Set to false if you want to send DOM tree information or other information that you do not want to convert.
ScriptCharset
Type: String
Applies only when the "script" transport is used (for example, the cross-domain "jsonp" or dataType options are "script" and "GET" types). The charset attribute is set on the script tag in the request. Usually only local and remote content encoding is not used at the same time.
StatusCode (default: {})
Type: PlainObject
A set of numeric HTTP code and function objects that are called in response. For example, if the response status is 404, the following alert is triggered:
$.ajax ({
StatusCode: {
404: function () {
Alert ("page not found")
}
}
})
If the request is successful, the function corresponding to the status code takes the same parameter as the success of the callback; if in an error result, they take the same parameter error callback.
(1.5 New)
Success
Type: Function (Object data, String textStatus, jqXHR jqXHR)
The callback function after a successful request. This function passes three parameters: the data returned from the server and processed according to the dataType parameter, a string describing the status, and the jqXHR (XMLHttpRequest before jQuery 1.4.x) object. In jQuery 1.5, a successful setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.
Timeout
Type: Number
Sets the request timeout in milliseconds. This setting will override the global settings in $.ajaxSetup (). The timeout period begins at the point in time when the $.ajax access is successful; if several other requests are in progress and the browser has no available connections, it may time out before it is sent. In jQuery 1.4.x and previous versions, the XMLHttpRequest object was in an invalid state if the request timed out; a member accessing any object might throw an exception. Only after Firefox 3.0 timeout can the script and JSONP requests be canceled; the script will run even if it arrives after the timeout.
Traditional
Type: Boolean
If you want to serialize the data in the traditional way, set it to true. Please refer to the jQuery.param method under the tool category.
Type (default: 'GET')
Type: String
Request method ("POST" or "GET"), default is "GET". Note: other HTTP request methods, such as PUT and DELETE, can also be used, but only some browsers support them.
Url (default: current page address)
Type: String
The address from which the request was sent.
Username
Type: String
User name in response to HTTP access authentication request
Xhr (default: when available ActiveXObject (IE), otherwise XMLHttpRequest)
Type: Function ()
The callback creates a XMLHttpRequest object. Defaults to ActiveXObject (IE) when available, otherwise XMLHttpRequest. Provide XMLHttpRequest or enhancement factories that cover your own implementation.
XhrFields
Type: PlainObject
A pair of "file name-file value" mappings that are used to set the native XHR object. For example, if necessary, you can use it to set withCredentials to true when making cross-domain requests.
$.ajax ({
Url: a_cross_domain_url
XhrFields: {
WithCredentials: true
}
})
In jQuery 1.5, the withCredentials attribute is not passed to the native XHR, so it can only be ignored for CORS requests that need to use this attribute. For this reason, we recommend that you use jQuery1.5.1+, if you need to use it.
After reading this, the article "methods used by jQuery.ajax" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.