1. 程式人生 > 其它 >Access to XMLHttpRequest at ‘http://localhost:3000/axios‘ from origin ‘null‘ has been blocked by COR

Access to XMLHttpRequest at ‘http://localhost:3000/axios‘ from origin ‘null‘ has been blocked by COR

技術標籤:vuejson

解決跨域時候發生的問題
在這裡插入圖片描述
在伺服器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’]);

next();
});