提起Ajax请求的方式(POST)

前言

=> 是ES6中的arrow function

x=>x+6

就相当于

function(x){
    return x+6;
}

正文

XMLHttpRequest

a=new XMLHttpRequest();
a.open("POST",url,true);
a.send("username=aaa");
a.onreadystatechange=function(){
    if(a.readystate==4&&a.status==200){
        alert(a.responseText);
    }
}

fetch

fetch(url,{
    method:"POST",
    headers:{"Content-Type:application/x-www-form-urlencoded"},
    body:"username=aaa"
}).then(function(res){
    return res.text();
}).then(function(data){
    alert(data);
})
    

fetch(url,{
    method:"POST",
    headers:{"Content-Type:application/x-www-form-urlencoded"},
    body:"username=aaa"
}).then(res=>res.text()).then(data=>alert(data))

上面两种是相同的

jQuery

$.post(url,{"username":"aaa"},function(text){
    alert(text);
})
$.ajax({
    type:"POST",
    url:url,
    data:"username=aaa",
    success:function(text){
        alert(text);
    }
})

猜你喜欢

转载自www.cnblogs.com/hf99/p/9807013.html