function ajax(method,url,data="",dataType="json"){ return new Promise((resolve,reject)=>{ //1 获取xhr var xhr= new XMLHttpRequest; //2 创建请求 xhr.open(method,url,true); //3 设置请求头 if(method=="post"){ xhr.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded"; )} //4 设置回调 xhr.onreadystatechange=function(){ if(xhr.readyState==4) if(xhr.status==200){ if(dataType=="json") resolve(JSON.parse(xhr.responseText)); else resolve(xhr.responseText); else reject("请求出错:"+xhr.status); } } //5 发送 xhr.send(data); }) }
原文地址:https://www.cnblogs.com/425500828zjy/p/10336155.html
时间: 2024-11-20 06:21:18