![](/img/trans.png)
[英]Can't get Mapbox SDK to recognize fuzzyMatch property in geocodeForward request
[英]I can't get golang to recognize my get request with parameters
我正在尝试在React中使用参数创建一个简单的axios get请求以使用Go。 不管我做什么,都要不断获取GET URL path not found(404)错误。 这是JS
import React, {Component} from 'react'
import axios from "axios"
class ShowLoc extends Component {
constructor(props){
super(props)
}
componentDidMount(){
const {id} = this.props.match.params
axios.get(`/loc/${id}`)
}
render() {
return(
<div>
Specific Location
</div>
)
}
}
export default ShowLoc
这是我的server.go文件的相关部分。 我正在使用大猩猩/多路复用器来识别参数
func main() {
fs := http.FileServer(http.Dir("static"))
http.Handle("/", fs)
bs := http.FileServer(http.Dir("public"))
http.Handle("/public/", http.StripPrefix("/public/", bs))
http.HandleFunc("/show", show)
http.HandleFunc("/db", getDB)
r := mux.NewRouter()
r.HandleFunc("/loc/{id}", getLoc)
log.Println("Listening 3000")
if err := http.ListenAndServe(":3000", nil); err != nil {
panic(err)
}
}
func getLoc(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(405), http.StatusMethodNotAllowed)
return
}
id := r
fmt.Println("URL")
fmt.Println(id)
}
因为从未找到我的get请求,所以我从未打过getLoc函数。 我应该如何从我的get请求中获取参数?
您尚未使用多路复用器路由器。 由于您将nil
传递给ListenAndServe
,因此它使用的是默认路由器,该路由器附加了所有其他处理程序。 而是向您的多路复用器路由器注册所有处理程序,然后将其作为第二个参数传递给ListenAndServe
。
func main() {
r := mux.NewRouter()
fs := http.FileServer(http.Dir("static"))
r.Handle("/", fs)
bs := http.FileServer(http.Dir("public"))
r.Handle("/public/", http.StripPrefix("/public/", bs))
r.HandleFunc("/show", show)
r.HandleFunc("/db", getDB)
r.HandleFunc("/loc/{id}", getLoc)
log.Println("Listening 3000")
if err := http.ListenAndServe(":3000", r); err != nil {
panic(err)
}
}
func getLoc(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(405), http.StatusMethodNotAllowed)
return
}
id := r
fmt.Println("URL")
fmt.Println(id)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.