AJAX study notes (MOOC)

1. Create XHR objects (XHR objects support IE6 and above)

var reques;

if(window.XMLHttpRequest){

    request=new XMLHttpRequest();

}else{

    request=new ActiveXObject("Microsoft.XMLHTTP");//支持IE5.6

}

二、HTTP

3. XHR send request

1.open(method,url,async)

method - GET/POST, indicating the request method

url - request address

async - requests are asynchronous by default, synchronous when set to false

2.send(string)

string - fill in the parameters, send the request, the general GET method is empty, the POST method is not empty, it is information

eg

request.open("POST","creat.php",true);

request.setRequestHeader("Content-type","application/x-www-form-urlencoded");//Set the request header to tell the server that the information submitted is a form, which must be written in the middle of open and send

request.send("name=xxx&sex=男");

4. XHR gets a response

1. Get the response value


2. Get notified when the response is successfully sent back - readyState property

Get the status of the server response by listening to the value of readyState

var request=new XMLHttpRequest();
request.open("POST","creat.php",true);
request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
request.send("name=xxx&sex=男");
request.onreadystatechange=function(){
if(request.readyState===4&&request.status===200){
//做一些事情,request.responseText
}
}

5. Examples

6. JSON

1.json parsing and verification

json parsing

var josnobj=JSON.parse(josondata);

alert(jsonobj.staff[0].name);

2. JSON verification URL

JSONLint

Seven, ajax in jQuery



8. Cross-domain

js is for your consideration of security. It is not allowed to call objects of other pages across domains. For example, js under the a.com domain name cannot operate objects under the b.com domain name.


what is cross domain

1) The main domain name is different

2) Different subdomains

3) The port number is different

4) Different protocols

2. Methods of handling cross-domain

1) Proxy

2) JSONP, use the script tag <script></script> to apply for data of another domain name (only GET requests are supported)

3)XHR2





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324359948&siteId=291194637