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