ajax代码流程笔记

ajax使用标准流程代码


GET方式

<script>
var xhr = new XMLHttpRequest();
//设置为GET方式
xhr.open('GET','data.php',true);
xhr.send();
xhr.onreadystatechange( function(){
	//当readystate为4表示请求完成
	if(xhr.readystate == 4)
		//当响应状态码为200~300或304(表示本地已有缓存)时表示获得到数据
		if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
			console.log( xhr.responceText );
			console.log( xhr.responceXML );
		}
});
</script>

POST方式

<script>
var xhr = new XMLHttpRequest();
//设置为POST方式
xhr.open('POST','data.php',true);
//POST提交的数据在send参数中提交
xhr.send('name=admin&passwd=admin');
xhr.onreadystatechange( function(){
	if(xhr.readystate == 4)
		if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
			console.log( xhr.responceText );
			console.log( xhr.responceXML );
		}
});
</scirpt>
posted @ 2020-08-01 13:18  aPSYCHO  阅读(113)  评论(0编辑  收藏  举报