In the previous article, I will add some details of the jquery.ajax. This article explains if the class Ajax methods are packaged into Jquery.ajax. The next article explains the details of each filter and distributor.
Why wrap it up?
We know that the ancient XMLHttpRequest is not supported across domains for the sake of homology policy. So, in the front-end, you want to dynamically load cross-domain JavaScript scripts, usually using a scenario called script DOM element, such as:
var Scriptelem = document.createelement ("script");
SCRIPTELEM.SRC = "Http://anydomain.com/A.js";
document.getElementsByTagName ("Head") [0].appendchild (Scriptelem);
Similarly, JSON cannot cross domains through XMLHttpRequest, so using the script DOM Element, the JSON is populated in a callback function to implement its cross-domain, that is, JSONP (JSON with padding, filled json, or Parameter json).
In fact, Jsonp is going to get the JSON callback function to tell the server by getting arguments, and then the server will splice a script that uses the callback function and parameters to the JSON data that is needed, such as:
Callback ({"Name": "Justany_whitesnow"});
The jquery team, of course, wants developers to develop without needing to cross the domain, just use it directly.
So they wrap XMLHttpRequest, Script DOM Element, JSONP, and use them as Ajax.
By the way, in fact, the modern browser (Firefox 3.5+, Safari 4+, chrome, etc.), through the xmlhttprequeest implementation of the Cors Cross-origin Resource, Cross source resource sharing) native support. That is, XMLHttpRequest in some browsers, you can actually cross domains, just set the Access-control-allow-origin in the HTTP Response header. For example, set the wildcard character *.
And IE8 also introduced the xdomainrequest also achieved cors.
But after all, some browsers do not, so, cough ... This cannot be a general scheme.
How do you wrap it up?
First we have a cottage XHR object, which is the Jqxhr object. Simulate the XHR object by adding send, abort to it.
However, we need to deal with the specific things before the implementation of different programs, so we need a prefilter mechanism (a) to deal with beforehand.
Then we need to know exactly which program to use to execute the whole process, so we need a distribution mechanism (transport) to get the final Jqxhr object.
Inspectprefiltersortransports
We found a function of jquery.ajax and distribution mechanism in the inspectprefiltersortransports.
Pre
-Filtration inspectprefiltersortransports (prefilters, S, Options, JQXHR);
..... Get transport
transport = Inspectprefiltersortransports (transports, S, options, JQXHR);