JQuery Ajax Solution for Cross-domain access _jquery

Source: Internet
Author: User
Tags getscript
The specific circumstances are:
I. Mutual access to the domain and subdomain: http://www.aa.com/and book.aa.com
Ii. Mutual access to this domain and other domains: http://www.aa.com/and http://www.bb.com/with IFRAME
Iii. Mutual access to this domain and other domains: http://www.aa.com/and http://www.bb.com/XMLHttpRequest Access Proxies
Iv. Mutual access to this domain and other domains: http://www.aa.com/and http://www.bb.com/Create dynamic scripts with JS
Today the main discussion of the 4th solution, this and the last difference is that the request is to use the <script> tag, this requirement is two domains are you to develop the line. The principle is JS file injection, in the domain within a to generate a JS tag, its SRC point to the request of another domain of a page b,b return data, you can directly return JS code. Because the SRC attribute of the script can be cross-domain.
Here we need to use the Jquery.getscript (URL, callback) method, the URL is the URL of the script file, the callback function is called after the script resource has been loaded and evaluated.
First create a JS file in bb.com, test.js
Copy Code code as follows:

var ojb = {msg: ' JS cross domain request succeeded '};

Then use $.getscript to load the Test.js script on the aa.com page
Copy Code code as follows:

$ (function () {
$.getscript (' Http://www.bb.com/test.js ', function () {
if (OJB) {
alert (obj.msg);
}
});
});

The greatest benefit of using the $.getscript function is the assurance that the callback function will be invoked after the script has finished loading.
Personally feel this method is simpler than using proxies and IFRAME.
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.