Get the App
SLTechnology News&Howtos  ›  Development  › 

JQuery.ajaxPrefilter case analysis

Shulou Source: shulou.com Published: 2022-06-01 07:35:09 09月12日 Update

This article mainly explains the "jQuery.ajaxPrefilter case analysis", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "jQuery.ajaxPrefilter case analysis" bar!

JQuery.ajaxPrefilter ([dataTypes], handler (options, originalOptions, jqXHR))

Description: sent before each request and processed by $. Ajax () before they are processed, set custom Ajax options or modify existing options.

DataTypes

Type: String

An optional string containing one or more space-separated data types

Handler (options, originalOptions, jqXHR)

Type: Function ()

A handler that sets the default value for future Ajax requests.

Register a typical pre-filter that uses $.ajaxPrefilter (), which looks like this:

$.ajaxPrefilter (function (options, originalOptions, jqXHR) {

/ / Modify options, control originalOptions, store jqXHR, etc

})

Under the following circumstances:

Options is the requested option

The originalOptions value is provided as an unmodified option for the Ajax method, so there is no default value in the ajaxSettings setting

JqXHR is the requested jqXHR object

When customizing options that need to be handled in advance, the pre-filter (Prefilters) is a perfect choice. Given the following code, for example, if the custom abortOnRetry option is set to true, calling $.ajax () will automatically abort the request for the same URL:

Var currentRequests = {}

$.ajaxPrefilter (function (options, originalOptions, jqXHR) {

If (options.abortOnRetry) {

If (currentRequests [options.url]) {

CurrentRequests [options.url] .abort ()

}

CurrentRequests [options.url] = jqXHR

}

})

Pre-filters (Prefilters) can also be used to modify existing options. For example, the following proxy server requests http://mydomain.net/proxy/ across domains:

$.ajaxPrefilter (function (options) {

If (options.crossDomain) {

Options.url = "http://mydomain.net/proxy/" + encodeURIComponent (options.url)

Options.crossDomain = false

}

})

If an optional dataTypes parameter is provided, the prefilter (prefilter) will only be valid for requests that satisfy the specified dataTypes. For example, the following applies only to the pre-filters given by JSON and script requests:

$.ajaxPrefilter ("json script", function (options, originalOptions, jqXHR) {

/ / Modify options, control originalOptions, store jqXHR, etc

})

The $.ajaxPrefilter () method can also redirect the request to another data type and return that data type. For example, if the URL contains the specified property set in the isActuallyScript () function, then set to a request for "script":

$.ajaxPrefilter (function (options) {

If (isActuallyScript (options.url)) {

Return "script"

}

})

This ensures that the request is considered to be of type "script" and that all pre-filters that are valid for the script data type will also be applied to it.

Thank you for your reading, the above is the content of "jQuery.ajaxPrefilter case analysis", after the study of this article, I believe you have a deeper understanding of the problem of jQuery.ajaxPrefilter case analysis, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Type filter instance instance analysis analysis data processing learning validity content situation method program facet same code typical function preprocessing parameter Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple OPPO Reno Redmi NVidia