ajax请求封装

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);
		
			
		
		
})
}

  

猜你喜欢

转载自www.cnblogs.com/425500828zjy/p/10336155.html