从URL中的前端ReactJ中检索Golang Server中的表单值



我正在尝试在我的前端react中以输入形式输入搜索查询,并在我的Golang服务器中检索该搜索查询。我目前正在端口3000上的开发服务器和端口5000上的Goland服务器上运行React。

当我输入查询时,我会看到URL更改为localhost:3000/?query =" honda"哪个是我想要的,但它不会显示在后端服务器中,我认为这是因为我使用了2个不同的服务器。我知道我可以在JSON对象中发送数据,但是我想知道如何从Golang服务器中的URL检索值(从Golang Server服务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)
}
...

method="post"添加到表单中。例如

<form action="/path/post" method="post" ...

最新更新