vue中出現跨域問題


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

 

相關網址:https://www.cnblogs.com/joyco773/p/11473963.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM