当web想请求本地的应用服务的时候,axios和Ajax请求都会报跨域请求。
使用XMLHttpRequest方法请求即可解决
const xmlHttp = (type,url) => {
return new Promise((resolve,reject)=>{
var xhr = new XMLHttpRequest();
xhr.open(type, url, true);
xhr.onload = (e) => {
if (xhr.readyState === 4 && xhr.status === 200) {
const data = JSON.parse(xhr.responseText);
resolve(data);
}
};
xhr.onerror = (err) => {
reject(err);
}
xhr.send();
})
}