LoginSignup
1
0

More than 3 years have passed since last update.

GoのEchoでオリジン間リソース共有の問題を解決

Last updated at Posted at 2020-11-25

やりたいこと

フロント側の http://localhost:8080/ からバックエンド側の http://localhost:8080/ を呼び出したい
しかし以下のエラーがフロント側で出ていたので、これを解決したい

Access to XMLHttpRequest at 'http://localhost:8000' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

オリジン管理ソース共有に関してはこの記事では解説しませんが、 こちらの記事 がわかりやすかったです

環境情報

  • vue
    • 2.6
  • go
    • 1.15
    • echoをサーバーとして使用

解決方法

以下の設定を追加

e := echo.New()

// 以下を追加
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
    AllowOrigins: []string{"http://localhost:8080"},
    AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodPost, http.MethodDelete},
}))

無事フロント側からアクセスできる様になりました!

参考

1
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
1
0