两种Ajax写法

Ajax:(Asynchronous Javascript And XML)简称为异步的js和xml
js中有两种写法:

//原生js写法
function show(){
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var result=xhr.responseText;
alert(result);
}	
}
xhr.open('get','demo/login',true);
xhr.send();
}
//jquery封装后的写法
//第一种写法
$("#btn").click(()=>{
$.ajax({
type:"GET",
url:"demo/login",
data:"uname=hansu",
success:(data)=>{alert(data);}
});
//第二种写法
$.get("demo/login",{uname:'hansu'},(data,status)=>{alert(data+":"+status);});
})
</script>

猜你喜欢

转载自blog.csdn.net/weixin_44003190/article/details/86258985