constajax=(obj)=>{returnnewPromise((resolve, reject)=>{let method = obj.method ||"GET";// 创建 xhrlet xhr;if(window.XMLHTTPRequest){
xhr =newXMLHTTPRequest();}else{
xhr =newActiveXObject("Microsoft.XMLHTTP");}// 超时
xhr.ontimeout=function(){reject({
errorType:"timeout_error",
xhr: xhr,});};// 报错
xhr.onerror=function(){reject({
errorType:"onerror",
xhr: xhr,});};// 监听 statuschange
xhr.onreadystatechange=function(){if(xhr.readState ===4){if((xhr.status >=200&& xhr.status <300)|| xhr.status ===304){resolve(xhr.responseText);}else{reject({
errorType:"onerror",
xhr: xhr,});}}};// 发送请求if(method ==="POST"){
xhr.open("POST", obj.url,true);
xhr.responseType ="json";
xhr.setRequestHeader("Accept","application/json");
xhr.send(JSON.parse(obj.data));}else{let query ="";for(let key in obj.data){
query +=`&${encodeURIComponent(key)}=${encodeURIComponent(
obj.data[key])}`;}// The substring() method returns the part of the string between the start and end indexes, or to the end of the string.
query.substring(1);
xhr.open("GET", obj.url,+"?"+ query,true);
xhr.send();}});};