1
我目前正在编写一个宁静的web服务器,我想从angular2前端进行测试。由于服务器托管在另一个域名,而开发我需要Access-Control-Allow-Origin: *
(我认为)。我尝试用大猩猩处理程序包,即实现了以下内容:尝试使用下面的卷曲请求服务器时允许使用大猩猩处理程序的起源
origins := handlers.AllowedOrigins([]string{"*"})
log.Fatal(http.ListenAndServe(":"+os.Getenv(util.Port),
handlers.LoggingHandler(os.Stdout, handlers.CORS(origins)(router))))
现在:
curl -H "Origin: http://example.com" \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: X-Requested-Width" \
-X OPTIONS --verbose localhost:8000
我得到一个选项在服务器上请求,它返回403.我也尝试添加标题和允许的方法:
handlers.AllowedHeaders([]string{"X-Requested-With"})
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "OPTIONS"})
但它没有区别。我该如何解决这个问题?
如果你不能得到它的工作,答案在http://stackoverflow.com/questions/12830095/setting-http-headers-in-golang/24818638#24818638似乎是解决这个问题的方法 – sideshowbarker
不幸的是,这样做不起作用,因为我也在使用gorilla mux路由器并设置函数方法,所以我必须单独包装每条路线 – tofiffe