Access to XMLHttpRequest at *** from origin‘***' has been blocked by CORS policy: Response to preflight request ...


Access to XMLHttpRequest at *** from origin ‘null’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource

報錯截圖:

 


 

報錯原因:

瀏覽器的同源策略約定我們只能在同源的情況下(即協議,域名,端口相同),才能成功請求數據,這里的目的url和http://localhost:3000/是非同源的,所以瀏覽器報錯,告訴我們跨域了。

 


 

解決方式:

當遇到這個問題時,我是使用了中間件代理跨域(基於react),通過啟用一個代理服務器,來接收目的接口提供的數據。

1、手動安裝 http-proxy-middleware中間件

npm i http-proxy-middleware

2、在根目錄創建一個setupProxy.js文件

const proxy = require('http-proxy-middleware')

module.exports = function(app){
    app.use(
        proxy("/webapi", {
            target: "https://name.52byte.com/",//目標地址
            changeOrigin: true //設置成true:發送請求頭中host會設置成target 
        })

    )
}

3、利用axios發送請求

axios.post(`/webapi/webbrowser/GetEnglishNameListForWeb`, {
                realName: "",
                 gender: "male",
                 initialLetter: "", 
                 spell: "0", 
                 pronounce: "0", 
                 popularity: "1", 
                 pageIndex: 0
            }).then((response) => {
                // handle success
                console.log(response.data)
            }).catch(error => {
                // handle error
                console.log(error)
            }).then(() => {
                // always executed
            });

4、最后,獲取數據成功啦


免責聲明!

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



猜您在找 Access to XMLHttpRequest at '…' from origin '…' has been blocked by CORS policy Access to XMLHttpRequest at xxxx from origin ‘null‘ has been blocked by CORS policy: has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource ajax post上傳數據時,前端出現的跨域權限問題:ccess to XMLHttpRequest at ‘’rom origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok st has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response. 跨域報錯has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request. ElementUI項目請求SpringBoot后台項目時提示:Access to XMLHttpRequest at **from origin ** has been blocked by CORS policy Access to XMLHttpRequest at '6%E6E6%B%9A%9' from origin from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol scheme 接口在dev環境報跨域問題(has been blocked by CORS policy:Response to preflight request doesn't pass access control check:No 'Access-Control-Allow-Origin' header ispresent on the requested resource.),qa環境正常
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM