首先要把項目中 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