A simple example of XMLHttpRequest Level 2

Source: Internet
Author: User

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

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.