Four Solutions for Javascript cross-origin requests _ javascript skills

Source: Internet
Author: User
If the requested domain name does not match this domain name, this is cross-origin. Due to the cross-origin vulnerability, normal cross-origin request methods cannot be requested, for more information, see. Under what circumstances will CORS occur?
Assume the domain name is: http://www.example.com.cn/
If the requested domain name does not match this domain name, this is a cross-origin scenario. Due to a cross-origin vulnerability, normal cross-origin request methods cannot be requested.
Solution:
1. window. name
1. The server returns

The Code is as follows:


Script window. name = '{"id": "3", "name": "leisure"}'; script
2. Define an iframe and add an onload event.<Iframe> <BR> <script type = "text/javascript"> <BR> var load = false; <BR> function iLoad () {<BR> if (load = false) {<BR> // if the request is processed in the same domain, iframe is reloaded again after the request is processed. <BR> document. getElementById ('iframe1 '). contentWindow. location = '/'; <BR> load = true; <BR >}else {<BR> // obtain the window. name content. You must process the same domain before accessing it! <BR> var data = document. getElementById ('iframe1 '). contentWindow. name; <BR> alert (data); // {"id": "3", "name": "leisure"} <BR> load = false; <BR >}< BR> script <BR> </p> <BR> 3. Define a form and set the target of the form to the id of iframe, then, submit the form <BR> <p class = "codetitle"> <U> </U> Code as follows: </p> <p class = "codebody" id = "code26605"> <BR> </p> <BR> <STRONG> 2. JSONP </STRONG> <BR> the server returns callback ({"id ": "3", "name": "leisure"}); <BR> <p class = "codetitle"> <U> </U> the code is as follows: </p> <p class = "codebody" id = "code4579"> <BR> <script type = "text/javascript"> <BR> function callback (data) {<BR> alert (data); <BR >}< BR> script <BR> <script type = "text/javascript" src =" http://www.example.com.cn/product.jsp?id=5&jsonp=callback "> Script <BR> </p> <BR> <STRONG> 3. jQuery. getJSON </STRONG> <BR> the server returns data in json format test ({"id": "3", "name": "leisure "}); the <BR> <p class = "codetitle"> <U> </U> code defined in the callback parameter of the test function is as follows: </p> <p class = "codebody" id = "code44100"> <BR> $. getJSON (url + "? Callback =? ", Data, function (data) {<BR >}< BR> </p> <BR> note callback =? This parameter must be included. jquery will automatically generate a function name to replace this question mark! JQuery. getJSON is actually implemented using the JSONP method. <BR> <STRONG> 4. flash cross-origin </STRONG> <BR> Add crossdomain. xml to the server <BR> http://www.example.com.cn/crossdomain.xml <BR> <p class = "codetitle"> <U> </U> the code is as follows: </p> <p class = "codebody" id = "code25537"> <BR> <? Xml version = "1.0"?> <BR> <cross-domain-policy> <BR> </cross-domain-policy>

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.