【发布时间】:2012-10-02 19:11:04
【问题描述】:
我正在尝试使用 Gorilla/rpc 包来设置 RPC 以接收请求并回复响应(显然)。
首先我尝试使用Gorilla/rpc 提供的示例
这是我的代码:
type HelloArgs struct {
Who string
}
type HelloReply struct {
Message string
}
type HelloService struct{}
func (h *HelloService) Say(r *http.Request, args *HelloArgs, reply *HelloReply) error {
reply.Message = "Hello, " + args.Who + "!"
return nil
}
func main() {
r := mux.NewRouter()
jsonRPC := rpc.NewServer()
jsonCodec := json.NewCodec()
jsonRPC.RegisterCodec(jsonCodec, "application/json")
jsonRPC.RegisterCodec(jsonCodec, "application/json; charset=UTF-8") // For firefox 11 and other browsers which append the charset=UTF-8
jsonRPC.RegisterService(new(HelloService), "")
r.Handle("/api", jsonRPC)
http.ListenAndServe(":"+port, nil)
}
我有几个问题:
我不确定如何设置
Access-Control-Allow-Origin标头,就像我通常在http.ResponseWriter(使用常规网络服务器)上针对跨域请求一样,因为这不需要http.ResponseWriter作为论据。为了访问
HelloService.Say方法,我实际上会发送什么?我试过{ method: "HelloService.Say", params:[{Who: "Me"}]},但得到405 (Method Not Allowed)(不确定这是不是因为我无法发出x域请求?)
非常感谢任何见解。
【问题讨论】:
-
尝试在数据中添加引号:{"method":"HelloService.Say","params":[{"Who":"Me"}]}