【发布时间】:2016-07-14 13:57:18
【问题描述】:
需要接受来自移动设备的 OPTIONS 方法, 尝试了多种方法并获得了奇怪的行为:
尝试此操作时,我从客户端收到 403:
(客户端在POST之前发送OPTIONS)
import (
"net/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/users", UserEndpoint)
r.HandleFunc("/projects", ProjectEndpoint)
methods := handlers.AllowedMethods([]string{"OPTIONS", "DELETE", "GET", "HEAD", "POST"}
http.ListenAndServe(":8000", handlers.CORS(methods)(r))
}
如果我省略了methods:
http.ListenAndServe(":8000", handlers.CORS()(r))
我得到 403 未授权
也玩过,去掉GET方法:
methods := handlers.AllowedMethods([]string{"OPTIONS"}
http.ListenAndServe(":8000", handlers.CORS(methods)(r))
但仍然可以
在浏览器(chromes DHC)中从休息客户端尝试时获得200 GET
但如果我删除 OPTIONS:
methods := handlers.AllowedMethods([]string{"DELETE", "GET", "HEAD", "POST"}
http.ListenAndServe(":8000", handlers.CORS(methods)(r))
我得到 405
第一个示例基于 gorilla handler 文档
对这个问题有什么想法吗?
谢谢
【问题讨论】: