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>