0
0

More than 1 year has passed since last update.

corsエラーが出た

Posted at

行っていたこと

ローカル開発でフロントエンドとバックエンドを接続する際にエラーが起きた。
fetchでデータを引っ張る時にブラウザのコンソールでエラーを発見!

フロント側:http://localhost:8111(webpack)
バックエンド側:http://localhost:8080(express)

エラー内容

Access to fetch at ... from origin ...has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
If an opaque response serves your needs, set the request's mode to 'no-cors' to 
fetch the resource with CORS disabled.

CORSにブロックされた...

対応

yarn add cors
app.ts

import cors from "cors";

const app = express();

const corsOptions = {
origin: "http://localhost:8111",
optionsSuccessStatus: 200,
};
app.use(cors(corsOptions));

最後に

バックエンド側でcorsを導入したら接続に成功しました!
※備忘録を兼ねて作成

参考記事

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