首先要把项目中 config 文件下的 index.js 找出来,
看一下是否已经写上了代理,for example:
proxyTable:{ //大概在当前文件13行的样子哈 '/api':{ target:'http://xxxxxxxx', // 调用接口域名和端口号哈 changeOrigin:true, pathRewrite:{ '^/api':'/api' } } }
错误场景一:
Access to XMLHttpRequest at 'xxxxx' from origin 'xxx' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
错误场景二:
Access to XMLHttpRequest at 'xxxx' from origin 'xxx' has been blocked by CORS policy:Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
目前遇到的这两种都跟后端有关,需要后端配合处理一下即可。
//配置请求
const baseUrl = 'xxxxx';
const options = {};
const url = baseUrl +'/api';
const params = {
'userPhone':'xxx'
}
options.method = 'post';
options.mode = 'no-cors';
options.body = JSON.stringify(params);
options.headers = {
'Content-Type':'application/json'
}
return fetch(url,options,{credentials:'include'}).then(res=>{}).catch(err=>{})
链接地址: https://www.cnblogs.com/mryaohu/p/12483757.html