JS face Question-----communication Class

Source: Internet
Author: User

Topic 1: What are homologous policies and limitations

Topic 2: How to communicate back and forth

Ajax WebSocket CORS

Topic 3: How to create Ajax

Workflow for XMLHttpRequest objects

Compatibility processing

Trigger conditions for events

The order in which events are triggered

Util.json =function(options) {varopt ={url:‘‘, Type:' Get ', Data: {}, success:function() {}, error:function () {},     };     Util.extend (opt, options); if(opt.url) {varXHR =XMLHttpRequest?NewXMLHttpRequest ():NewActiveXObject (' microsoft.xmlhttp '); vardata =opt.data, URL=opt.url, Type=opt.type.toUpperCase (), Dataarr= [];  for(varKinchData) {dataarr.push (k+ ' = ' +data[k]); }         if(type = = = ' GET ') {url= URL + '? ' + dataarr.join (' & ')); Xhr.open (type, Url.replace (/\?$/g, "),true);         Xhr.send (); }         if(type = = = ' POST ') {xhr.open (type, url,true); Xmlhttp.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded '); Xhr.send (dataarr.join (' & ')); } xhr.onload=function () {             if(xhr.status = = = | | xhr.status = = 304) {                 varres; if(opt.success && opt.successinstanceofFunction) {res=xhr.responsetext; if(typeofres = = = ' String ') {res=Json.parse (res);                     Opt.success.call (xhr, res); }                 }             } Else {                 if(opt.error && Opt.errorinstanceofFunction)                 {opt.error.call (xhr, res);     }             }         }; } };

Topic 4: Several ways to communicate across domains

JSONP Hash PostMessage WebSocket CORS

JSONP (most commonly used, not explained Here)

Hash 

// using hash, The scene is the current page A through the IFRAME or frame embedded Cross-domain page B      // the pseudo code in a is as Follows:      var B = document.getElementsByTagName (' iframe ');       = B.src + ' # ' + ' data ';       // the Pseudo-code in B is as follows      function () {          var data = window.location.hash;      };

PostMessage

// postMessage      // window A (http:a.com) sends information to a cross-domain window B (http:b.com)      Bwindow.postmessage (' Data ', ' http://B.com ');       // Listening in window b      function (event) {          console.log (event.origin); // http:a.com          Console.log (event.source); // Awindow          Console.log (event.data); // Data      false);

  WebSocket

//Websocket "references" http://www.ruanyifeng.com/blog/2017/05/websocket.html      varWS =NewWebSocket (' wss://echo.websocket.org '); Ws.onopen=function(evt) {console.log (' Connection open ... '); Ws.send (' Hello websockets! ');      }; Ws.onmessage=function(evt) {console.log (' Received Message: ', evt.data);      Ws.close ();      }; Ws.onclose=function(evt) {console.log (' Connection Closed. '); };

  CORS

// CORS "reference" http://www.ruanyifeng.com/blog/2016/04/cors.html      // URL (required), options (optional)      Fetch ('/some/url/', {          ' get ',      }). then (function  (response) {      }). Catch (function  (err) {        //  error, equivalent to then second argument, but this is better to be more intuitive      });

JS face Question-----communication Class

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.