0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【React】Corsエラーの回避 メモ

Posted at

現状

React+Expressの環境でwebアプリを構築していますが、corsエラーが出ています。
エラーの中身↓

Access to XMLHttpRequest at 'apiのurl' from origin 'front側のurl' 
has been blocked by CORS policy:
The 'Access-Control-Allow-Origin' header contains the invalid value 'front側のurl'.

corsエラーが出ています。

ひとまずの対応

const express = require("express");
const cors = require("cors");
const app = express();

app.use(
  cors({
    origin: "*",
    credentials: true, //レスポンスヘッダーにAccess-Control-Allow-Credentials追加
    optionsSuccessStatus: 200, //レスポンスstatusを200に設定
  })
);

urlの指定を*にしているのでセキュリティ的に少しアウトな部分もあるのですが、とりあえずこれで対応します。
なぜかurlを指定するとエラーになってしまうのでそれができたら追記します。
(わかる方いたらコメントお願いします)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?