让golang Gorilla CORS处理程序工作 [英] Making golang Gorilla CORS handler work
问题描述
我在这里有相当简单的设置,如下面的代码所述。但是我无法使 CORS
工作。我一直得到这个错误:
XMLHttpRequest无法加载 HTTP://本地主机:3000 /注册。对
预检请求的响应未通过访问控制检查:否请求的资源上存在'Access-
Control-Allow-Origin'标头。因此不允许访问源
源' http:// localhost:8000 '。
响应的HTTP状态码为403.
我相信我在这里错过了一些简单的东西。
以下是我的代码:
包主
进口(
记录
净/ http
github.com/gorilla/handlers
github.com/gorilla/mux
myApp / src / controllers
)
func main(){
ac:= new(controllers.AccountController)
router := mux.NewRouter()
router.HandleFunc(/ signup,ac.SignUp).Methods(POST)
router.HandleFunc(/ signin,ac.SignIn)。方法(POST)
log.Fatal(http.ListenAndServe(:3000,handlers.CORS()(router)))
}
$ c $请阅读马库斯建议的链接,以及关于什么触发CORS飞行前请求的链接。
提前请求:您可能拥有JSON之类的内容类型,您的服务器可能无法处理。如果您在前端使用了常见的AJAX,请尝试添加此AJAX: https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requested-With
Gorilla的 handlers.CORS() code>将设置理智的默认值以获得CORS的基础知识为您工作;然而,你可以(也许应该)以更实用的方式进行控制。
以下是一些入门代码:
headersOk:= handlers.AllowedHeaders([] string {X-Requested-With})
originsOk:= handlers.AllowedOrigins([] string {os.Getenv( ORIGIN_ALLOWED)})
methodsOk:= handlers.AllowedMethods([] string {GET,HEAD,POST,PUT,OPTIONS})
// start server listen
//带有错误处理
log.Fatal(http.ListenAndServe(:+ os.Getenv(PORT),handlers.CORS(originsOk,headersOk,methodsOk)(路由器)))
I have fairly simple setup here as described in the code below. But I am not able to get the CORS
to work. I keep getting this error:
XMLHttpRequest cannot load http://localhost:3000/signup. Response to
preflight request doesn't pass access control check: No 'Access-
Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8000' is therefore not allowed access. The
response had HTTP status code 403.
I am sure I am missing something simple here.
Here is the code I have:
package main
import (
"log"
"net/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"myApp/src/controllers"
)
func main() {
ac := new(controllers.AccountController)
router := mux.NewRouter()
router.HandleFunc("/signup", ac.SignUp).Methods("POST")
router.HandleFunc("/signin", ac.SignIn).Methods("POST")
log.Fatal(http.ListenAndServe(":3000", handlers.CORS()(router)))
}
解决方案 Please read the link Markus suggested, and also about what triggers CORS pre-flight requests.
Pre-flight requests: You may have a content type like JSON, or some other custom header that's triggering a pre-flight request, which your server may not be handling. Try adding this one, if you're using the ever-common AJAX in your front-end: https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requested-With
Gorilla's handlers.CORS()
will set sane defaults to get the basics of CORS working for you; however, you can (and maybe should) take control in a more functional manner.
Here's some starter code:
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With"})
originsOk := handlers.AllowedOrigins([]string{os.Getenv("ORIGIN_ALLOWED")})
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
// start server listen
// with error handling
log.Fatal(http.ListenAndServe(":" + os.Getenv("PORT"), handlers.CORS(originsOk, headersOk, methodsOk)(router)))
这篇关于让golang Gorilla CORS处理程序工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!