Three common methods for js cross-origin request data _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces three common methods for js cross-origin request data. For more information, see the impact of the same-origin JavaScript policy, or the url under different ports for the same domain name will become an unsupported cross-origin request.
How can we solve this problem at this time? I have made some PREPARATIONS FOR THE cainiao bald head:
1. JavaScript
In the case of native js (without jQuery and ajax support), the client code is usually like this (I suppose it is the http: // localhost: 8080/webs/I .mediapower.mobi/wutao/index.html ):

《script》  var xhr = new XMLHttpRequest();  xhr.open("get", "http://i2.mediapower.mobi/adpower/vm/Bora/js/test.js?1.1.23", true);  xhr.onreadystatechange = function() {    if (xhr.readyState == 4 && xhr.status == 200) {      console.log(xhr.responseText);    }  };  xhr.send(null);《script》

Save, open http: // localhost: 8080/webs/I .mediapower.mobi/wutao/index.html in the browser, and open the leleconsole:


The browser returns a same-source restriction error, which means that you cannot request url data across domains.
Then, I will first adopt the first policy to use the script tag in html to insert the js script:
(1) Use the script tag to reference and write the src url you need,For example:

Script var callbackfunction = function (data) {console. log ('data from my cross-origin requests --> '+ data. name) ;}; script
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.