600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > GO + React + Axios Response to preflight request doesn't pass access control check: It does not hav

GO + React + Axios Response to preflight request doesn't pass access control check: It does not hav

时间:2019-04-17 11:41:51

相关推荐

GO + React + Axios  Response to preflight request doesn't pass access control check: It does not hav

使用Go + Reat 使用 Axios 请求后端, 出现:

Access to XMLHttpRequest at 'http://127.0.0.1:20002/v1/user/login' from origin 'http://localhost:4444' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

后端是:

c.Writer.Header().Add("Access-Control-Allow-Origin", "*")c.Writer.Header().Add("Access-Control-Allow-Credentials", "true")c.Writer.Header().Add("Access-Control-Allow-Headers", "Content-Type,Access-Control-Allow-Headers,Content-Length,Accept,Authorization,X-Requested-With")c.Writer.Header().Add("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS")cCp := c.Copy()path := cCp.Request.URL.Pathmethod := cCp.Request.Methodlog.Info("http - " + c.ClientIP() + " " + path + " " + method)

然后网上找啊找啊, 有一个是:

package.json 这个文件里面添加

"proxy": {"/*": {"target": "http://127.0.0.1:20002","changeOrigin":true}},

不行, 我只想说, 没有进过自己去检测 , 就随便的搞, 这样我的还是不行,

最后修改后端实现:

c.Writer.Header().Add("Access-Control-Allow-Origin", "*")c.Writer.Header().Add("Access-Control-Allow-Credentials", "true")c.Writer.Header().Add("Access-Control-Allow-Headers", "Content-Type,Access-Control-Allow-Headers,Content-Length,Accept,Authorization,X-Requested-With")c.Writer.Header().Add("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS")cCp := c.Copy()path := cCp.Request.URL.Pathmethod := cCp.Request.Methodlog.Info("http - " + c.ClientIP() + " " + path + " " + method)if cCp.Request.Method == "OPTIONS" {c.Writer.WriteHeader(http.StatusOK)return}else{c.Next()}

老哥, 稳得很.就可以了

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。