<script>
document.onclick = function(){
var dizhi = "http://localhost/ajax/data/data.php";
ajaxPost(dizhi,function(res){
console.log(res);
},"abc=987&zxc=hello");
}
function ajaxPost(url,cb,data){
var xhr = new XMLHttpRequest();
xhr.open("POST",url);
// 设置发送头信息,中,发送数据的格式,为,表单数据格式
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(data);
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status === 200){
cb(xhr.responseText);
}
}
}
</script>
ajax---post函数封装
最新推荐文章于 2021-08-05 14:24:44 发布