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

标签 reactjs go redirect server proxy

我在 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/

相关文章:

javascript - 使用 javascript 将 php 变量发布到移动网站 - 空白变量

jsp - 使用 scriplet 代码从 jsp 重定向

javascript - 即使我不从外部链接调用,React-native 链接 getinitialurl() 也能正常工作

javascript - 为什么我在 React 应用程序的 HTML 输出中看到传递的 Prop ?

go - 使用 Go 范围语法的意外行为

xml - 不打印 XML 输出,而是创建 XML 文件

reactjs - Apollo 客户端与 GatsbyJS 集成时遇到困难

javascript - react : Is a component entirely re-rendered on every setState()?

sockets - 从 go 中的套接字读取时遇到问题

javascript - 如何在表单提交时重定向页面