Access to XMLHttpRequest at ‘http://localhost:3000/axios‘ from origin ‘null‘ has been blocked by COR
阿新 • • 發佈:2021-02-03
解決跨域時候發生的問題
在伺服器js檔案中修改跨域訪問
原先
// 設定允許跨域訪問該服務 app.all('*', function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header('Access-Control-Allow-Headers', 'Content-Type'); res.header('Access-Control-Allow-Headers', 'mytoken'); next(); });
解決
// 設定允許跨域訪問該服務
app.all(’’, function (req, res, next) {
res.header(“Access-Control-Allow-Origin”, "");
res.header(‘Access-Control-Allow-Methods’, ‘PUT, GET, POST, DELETE, OPTIONS’);
res.header(“Access-Control-Allow-Headers”, “X-Requested-With”);
res.header(‘Access-Control-Allow-Headers’, [‘Content-Type’, ‘mytoken’]);
});