【问题标题】:How to redirect to another webpage如何重定向到另一个网页
【发布时间】:2021-05-10 16:57:29
【问题描述】:

我想做的事
注册成功后,我想重定向到索引页面。

有什么问题
填写用户名和密码后,页面不会重定向到索引页面,而是停留在signup.html。

我做了什么
我怀疑 createUser 函数返回 nil 并导致错误,但我无法解决。

代码

// User model declaration
type User struct {
    gorm.Model
    Username string `form:"username" binding:"required" gorm:"unique;not null"`
    Password string `form:"password" binding:"required"`
}
// User sign up
func createUser(username string, password string) []error {
    passwordEncrypt, _ := crypto.PasswordEncrypt(password)
    db := gormConnect()
    defer db.Close()
    // Insert
    if err := db.Create(&User{Username: username, Password: passwordEncrypt}).GetErrors(); err != nil {
        return err
    }
    return nil
}

func main(){
router := gin.Default()
    router.LoadHTMLGlob("front/*.html")

    dbInit()

    // index
    router.GET("/", func(c *gin.Context) {
        b_models := dbGetAll()
        num := dbGetNum()
        sumQuantity := dbGetSumQuantity()
        sumPrice := dbGetSumPrice()
        c.HTML(200, "belongings.html", gin.H{"b_models": b_models, "num": num, "sumQuantity": sumQuantity.Totalquantity, "sumPrice": sumPrice.Totalprice})
    })

    // User sign up page
    router.GET("/signup", func(c *gin.Context) {
        c.HTML(200, "signup.html", gin.H{})
    })

    // User sign up process
    router.POST("/signup", func(c *gin.Context) {
        var form User
        // Validation
        if err := c.Bind(&form); err != nil {
            c.HTML(http.StatusBadRequest, "signup.html", gin.H{"err": err})
            log.Println("fail to login because your info is invalid")
            c.Abort()
        } else {
            username := c.PostForm("username")
            password := c.PostForm("password")
            // Process to reject duplicate registered users
            if err := createUser(username, password); err != nil {
                c.HTML(http.StatusBadRequest, "signup.html", gin.H{"err": err})
                c.Abort()
            } else {
                log.Println("success to signup!")
                c.Redirect(302, "/")
            }
        }
    })
}

【问题讨论】:

  • 您好,您是否将索引页绑定到任何路由?
  • 你的重定向ajax请求到/
  • 嗨@meshkati 是的,我做到了。我在下面写了这段代码。 // index router.GET("/", func(c *gin.Context) { b_models := dbGetAll() num := dbGetNum() sumQuantity := dbGetSumQuantity() sumPrice := dbGetSumPrice() c.HTML(200, "belongings.html", gin.H{"b_models": b_models, "num": num, "sumQuantity": sumQuantity.Totalquantity, "sumPrice": sumPrice.Totalprice}) })
  • 可以打开chrome开发者工具developers.google.com/web/tools/chrome-devtools/open,然后切换到network选项卡查看响应是否正确(http响应码为302,Location header为/ )
  • @mkopriva 非常感谢。我可以解决这个问题并在 golang 中学到很多关于 nil 的知识。

标签: go redirect go-gin


【解决方案1】:

你可以用javascript改变页面,我不明白你为什么要你的后端做前端的东西。

window.location.href = "the_page.html"

【讨论】:

  • 感谢您的评论。我在后端做了,因为我想做注册过程,它有业务逻辑。
  • 我仍然建议您根据后端的响应更改前端页面
  • 是因为响应速度和内存效率吗?
猜你喜欢
  • 2015-12-07
  • 1970-01-01
  • 2021-06-09
  • 2019-08-10
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多