Qa:
1. Handwritten Ajax
The following are already compatible with IE
functionAjax (type, URL, data, success, failed) {//creating an Ajax object varXHR =NULL; if(window. XMLHttpRequest) {XHR=NewXMLHttpRequest (); } Else{XHR=NewActiveXObject (' Microsoft.XMLHTTP ') } varType =type.touppercase (); //used to clear the cache varRandom =Math.random (); if(typeofdata = = ' object '){ varstr = "; for(varKeyinchdata) {STR+ = key+ ' = ' +data[key]+ ' & '; } Data= Str.replace (/&$/, "); } if(Type = = ' GET '){ if(data) {Xhr.open (' GET ', url + '? ' + data,true); } Else{Xhr.open (' GET ', url + '? t= ' + random,true); } xhr.send (); } Else if(Type = = ' POST ') {Xhr.open (' POST ', url,true); //If you need to POST data like an HTML form, use setRequestHeader () to add an HTTP header. Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Xhr.send (data); } //Processing return DataXhr.onreadystatechange =function(){ if(Xhr.readystate = = 4) {//Status changeif(Xhr.status = = 200{//The status code returned by the server success (Xhr.responsetext); } Else { if(Failed) {failed (xhr.status); } } } }} //Test CallvarSendData = {name: ' Asher ', Sex: ' Male '}; Ajax (' Get ', ' data/data.html ', SendData,function(data) {Console.log (data);},function(Error) {Console.log (error);});
ReadyState every time the change starts, the execution of the onreadystatechange function
0, uninitialized, not yet called the Send () method
1, load, call Send (), sending request
2, loading complete, send () execution completed, and has accepted the corresponding content
3. Responding is being parsed
4, the response resolution is complete, the client calls
Status code
200-Successful request
3xx-Redirect, Direct browser jump
4xx-Client request error (404 Not Found, 401 does not have permission)
5xx-Server Error
2. Cross-domain Implementation method
Cross-domain: Browser-origin policy, which does not allow AJAX access to other interfaces
Cross-Domain condition: protocol, domain name, port, there is a difference, even if the cross-domain
You can cross-domain tags:
< img src =xxx >< link href = xxx>< script src = xxx>
Implementation mode: JSONP, Server Set HTTP header
JavaScript Basics--ajax