Javascript 向XMLHttpRequests添加参数会导致405错误
我可以使用以下代码成功发送无参数的XMLHttpRequest: 模板文件:Javascript 向XMLHttpRequests添加参数会导致405错误,javascript,go,xmlhttprequest,Javascript,Go,Xmlhttprequest,我可以使用以下代码成功发送无参数的XMLHttpRequest: 模板文件: xhr.open('POST', '/mytemplate'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onload = function() { if (xhr.status == 200) { alert('success'); } else { aler
xhr.open('POST', '/mytemplate');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
if (xhr.status == 200) {
alert('success');
}
else {
alert('failed:' + xhr.status);
}
}
xhr.send();
xhr.open('POST', '/mytemplate?param1='+var1+'¶m2='+var2+'¶m3='+var3);
路由器:
r.HandleFunc("/mytemplate", MyHandler).Methods("POST")
r.HandleFunc("/mytemplate/{param1}/{param2}/{param2}", MyHandler).Methods("POST")
但当我尝试添加参数时,我得到了一个405错误。我只更改了两行,不确定我做错了什么:
模板文件:
xhr.open('POST', '/mytemplate');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
if (xhr.status == 200) {
alert('success');
}
else {
alert('failed:' + xhr.status);
}
}
xhr.send();
xhr.open('POST', '/mytemplate?param1='+var1+'¶m2='+var2+'¶m3='+var3);
路由器:
r.HandleFunc("/mytemplate", MyHandler).Methods("POST")
r.HandleFunc("/mytemplate/{param1}/{param2}/{param2}", MyHandler).Methods("POST")
open命令中的参数定义为URL查询参数,所有参数都在?之后?。 但是您在对r.HandleFunc的调用中添加了它们作为URL路径参数 尝试使用r.HandleFunc处理/mytemplate的基本路径,然后使用r.URL.query解析MyHandler函数中的查询参数。这将返回查询参数的映射,您应该能够通过键param1、param2…引用该映射 下面是一个简单的例子:
func myHandler(w http.ResponseWriter, r *http.Request) {
//get the query vals
values := r.URL.Query()
//parse specific params
param1 := values["param1"]
//do other stuff
}
func main() {
//define the handler
http.HandleFunc("/myHandlerPath", myHandler)
// do some other stuff?
// ...
// serve up the endpoint
log.Fatal(http.ListenAndServe(":8081", nil))
}
open命令中的参数定义为URL查询参数,所有参数都在?之后?。 但是您在对r.HandleFunc的调用中添加了它们作为URL路径参数 尝试使用r.HandleFunc处理/mytemplate的基本路径,然后使用r.URL.query解析MyHandler函数中的查询参数。这将返回查询参数的映射,您应该能够通过键param1、param2…引用该映射 下面是一个简单的例子:
func myHandler(w http.ResponseWriter, r *http.Request) {
//get the query vals
values := r.URL.Query()
//parse specific params
param1 := values["param1"]
//do other stuff
}
func main() {
//define the handler
http.HandleFunc("/myHandlerPath", myHandler)
// do some other stuff?
// ...
// serve up the endpoint
log.Fatal(http.ListenAndServe(":8081", nil))
}
您正在处理的路线如下所示:
/mytemplate/some/variables/here
/mytemplate?param1=some¶m2=variables¶m3=here
但是,您正在使用查询参数将请求发送到/mytemplate,如下所示:
/mytemplate/some/variables/here
/mytemplate?param1=some¶m2=variables¶m3=here
大猩猩Mux在这方面不匹配。如果要匹配查询参数而不是URL参数,请参阅Gorilla Mux中的.querys方法。您正在处理的路由如下所示:
/mytemplate/some/variables/here
/mytemplate?param1=some¶m2=variables¶m3=here
但是,您正在使用查询参数将请求发送到/mytemplate,如下所示:
/mytemplate/some/variables/here
/mytemplate?param1=some¶m2=variables¶m3=here
大猩猩Mux在这方面不匹配。如果您想匹配查询参数,而不是URL参数,请参阅Gorilla Mux中的.querys方法。如果我匹配了r.URL.query,那么这不适用于我的所有路由器,而不仅仅是我要处理其参数的特定路由器吗?你能提供一个代码示例吗?我有点困惑。我建议您在处理函数中为正在处理的特定路由调用处理函数,以便从处理函数中的请求指针读取查询参数。啊,抱歉,完全误解了您的意思。谢谢!没问题!示例总是有帮助的:如果我做了r.URL.Query,那么这不适用于我所有的路由器,而不仅仅是我想要处理的特定路由器的参数吗?你能提供一个代码示例吗?我有点困惑。我建议您在处理函数中为正在处理的特定路由调用处理函数,以便从处理函数中的请求指针读取查询参数。啊,抱歉,完全误解了您的意思。谢谢!没问题!示例总是有帮助的: