JQuery Ajax: Wrapping A class of Ajax methods

Source: Internet
Author: User
Tags json resource domain

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);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.