First, set the target Website Based on the URL provided later to allow cross-origin access: Mozilla, W3C, and msdn.
The newly designed cross-origin scheme is extremely good. IE8 is an xdomainrequest, firefox3.5, safari4, chrome 2, and so on. It follows the original XMLHTTPRequest object and they all have the same method to process various callbacks:
- Onload: called when the request is successful.
- Onerror: called when the request fails.
- Onabort: called when a request is interrupted (using the abort method)
Therefore, this cross-origin request is very simple!
If ("1" [0]) {// only IE8 and newer standard browsers are allowed to enter the following logic var xhr = Window. xdomainrequest? New xdomainrequest: New XMLHttpRequest; try {xhr. onload = function () {// because the returned JSON is too long, we truncated some alert ([xhr. responsetext. slice (), xhr]) ;}; xhr. open ("get", "http://ss-o.net/json/wedataAutoPagerize.json"); xhr. send ();} catch (e) {alert ("request failed:" + E. message );}}
<Br/> <! Doctype HTML> <br/> <pead> <br/> <title> XMLHttpRequest Level 2 by situ zhengmei </title> <br/> <Meta charset = "UTF-8"/> <br/> <meta content = "Ie = 8" http-equiv = "X-UA-compatible"/> <br/> <meta name = "keywords" content = "XMLHttpRequest Level 2 by situ zhengmei"/> <br/> <meta name = "Description" content = "XMLHttpRequest Level 2 by situ zhengmei"/> <br/> <SCRIPT type = "text/JavaScript" charset = "UTF-8"> </P> <p> If ("1" [0]) {// only Allow IE8 and newer standard browsers to enter the following logic <br/> var xhr = Window. xdomainrequest? New xdomainrequest: New XMLHttpRequest; <br/> try {<br/> xhr. onload = function () {<br/> // because the returned JSON is too long, we truncated it during demonstration. <br/> alert ([xhr. responsetext. slice (), xhr]); <br/>}; <br/> xhr. open ("get", "http://ss-o.net/json/wedataAutoPagerize.json"); <br/> xhr. send (); <br/>}catch (e) {<br/> alert ("request failed:" + E. message ); <br/>}</P> <p> </SCRIPT> <br/> </pead> <br/> <body> <br/> <p> XMLHttpRequest Level 2 by situ zhengmei </p> <br/> <p> <a href = "http://ss-o.net/json/wedataAutoPagerize.json"> http://ss-o.net/json/wedataAutoPagerize.json </a> </P> <br/> <p> <a href = "http://ss-o.net/json/wedataLDRize.json"> http://ss-o.net/json/wedataLDRize.json </a> </P> <br/> </body> <br/> </ptml> <br/>
RunCode