我正尝试在前端React中的输入表单中输入搜索查询,并在Golang服务器中检索该搜索查询。我目前在端口3000上的开发服务器和端口5000上的Goland服务器上运行React。
当我输入查询时,我看到URL更改为localhost:3000/? query ="Honda“,这是我想要的,但它没有显示在后端服务器中,我认为这是因为我使用了两个不同的服务器。我知道我可以在json对象中发送数据,但我想知道如何从Golang服务器的URL中检索值(从golang服务器提供React文件)。
// React front end search bar component
<Form action="/path/post" id="searchform" onSubmit={this.handleSubmit}>
<Input value={this.state.query} onChange={this.handleChange} type="text" name="query" />
// React package.json
...
"proxy": "http://localhost:5000",
...
- - - - - - - - - - - - - - - - -
// golang server
...
func main() {
router := gin.Default()
router.Use(static.Serve("/", static.LocalFile("../client/public", true)))
ping := router.Group("/path")
ping.POST("/post", pingFunc)
router.Run(":5000")
}
func post(c *gin.Context) {
c.Request.ParseForm()
q := FormValue("query")
fmt.Println(q)
}
...发布于 2019-05-09 01:31:23
将method="post"添加到表单。例如
<form action="/path/post" method="post" ...https://stackoverflow.com/questions/56042398
复制相似问题