モバイルアプリでWebAPIを使用し、DBからデータを取得するプログラムを書いています。
クロスドメイン制約でリクエストを送ることができないため、解決したいです。
尚、"github.com/labstack/echo"はdep ensure --addでフェッチしましたが、
"github.com/labstack/echo/middleware"は下記のエラーが出たため、go getでフェッチしています。
** ✗ nothing to -add, github.com/labstack/echo is already in Gopkg.toml and the project's direct imports or required list**
・router.go
Go
1package route 2 3import ( 4 "echo-sample/controller" 5 6 "github.com/labstack/echo" 7 8 "github.com/labstack/echo/middleware" 9) 10 11func Init() *echo.Echo { 12 13 e := echo.New() 14 e.Use(middleware.CORS()) 15 16 api := e.Group("/api") 17 { 18 api.GET("/members", controller.GetMembers()) 19 } 20 return e 21}
一部抜粋ですが、下記コードでレスポンス内容を確認したいです。(現在はアラートが出ません)
js
1 let oReq = new XMLHttpRequest(); 2 oReq.open('GET', 'http://localhost:1323/api/members'); 3 oReq.send(); 4 oReq.addEventListener('load', function(event){ 5 6 window.alert(event.target.status); 7 window.alert(event.target.responseText); 8 });
エラー
router.goのe.Use(middleware.CORS())で発生
cannot use middleware.CORS() (type "github.com/labstack/echo".MiddlewareFunc) as type "echo-sample/vendor/github.com/labstack/echo".MiddlewareFunc in argument to e.Use
試したこと
https://echo.labstack.com/cookbook/cors
https://echo.labstack.com/middleware/cors
上記を参考にし、コード修正
あなたの回答
tips
プレビュー