JavaScript Basics--ajax

Source: Internet
Author: User

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

Related Article

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.