gpt4 book ai didi

reactjs - 服务器重定向不适用于 ReactJS 代理服务器

转载 作者:IT王子 更新时间:2023-10-29 02:06:55 26 4
gpt4 key购买 nike

我在 React 前端使用代理服务器,在后端使用 Go 服务器。当我不使用 react 或代理服务器时,重定向工作正常。但是,当我使用 React 及其开发服务器和我的 Go 后端的代理时,重定向不会执行任何操作。

// React package.json file 
"proxy": "http://localhost:5000", // proxy to golang hosted on 5000
"scripts": {
"start": "react-scripts start",
},

-----------------------

// golang server

func main() {
router := gin.Default()
router.Use(static.Serve("/", static.LocalFile("../client/public", true)))
ping := router.Group("/path")
ping.GET("/ping", pingFunc)
ping.POST("/ping", pingFuncPost)
router.Run(":5000")
}

// This redirect is not working.
// In the terminal it shows that a redirect is made but on the frontend
// nothing occurs

func pingFuncPost( c *gin.Context)
{
http.Redirect(c.Writer, c.Request, "/page", http.StatusSeeOther)`
}

最佳答案

你重定向,用

        const data = {"query": this.state.query};
fetch(`/path/ping`, { // should hit the end point of pingFuncPost in golang server, which should redirect to localhost:3000/results
method: 'POST',
body: JSON.stringify(data),
})
.then(res => res.json())
.then(res => {
<Redirect to="/somewhere/else" />
})

关于reactjs - 服务器重定向不适用于 ReactJS 代理服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56030553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com