Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirect is not success #3903

Open
DylanZhu2021 opened this issue Mar 24, 2024 · 1 comment
Open

redirect is not success #3903

DylanZhu2021 opened this issue Mar 24, 2024 · 1 comment

Comments

@DylanZhu2021
Copy link

this is my code:
c.HTML(http.StatusOK, "updateRecord.html", gin.H{
"patientId": record.PatientId,
"patientName": record.PatientName,
"doctorId": record.DoctorId,
"doctorName": record.DoctorName,
})
c.Redirect(http.StatusFound, "/admin/update")

I want redirect web page to ''/admin/update'',and sent some data to ''/admin/update'', ''/admin/update'' have a html file:updateRecord.html, but this way is wrong.Actually,the page have not redirect. Can you help me?

@RedCrazyGhost
Copy link
Contributor

Hello my friend, do you want to redirect the request parameters to the new html?

If so, please refer to the code example below:

package main

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.LoadHTMLFiles("test.tmpl")

	r.GET("/", func(c *gin.Context) {
		c.Redirect(http.StatusPermanentRedirect,"/html")
	})

	r.GET("/html", func(c *gin.Context) {
		data := struct {
			Test string `json:"test"`
		}{}

		if err := c.ShouldBindJSON(&data); err != nil {
			c.String(http.StatusInternalServerError, "body type is not JSON!")
			return
		}

		fmt.Println(data)
		
		c.HTML(http.StatusOK, "test.tmpl", gin.H{
			"test": data.Test,
		})
	})

	r.Run()
}
{{define "test.tmpl"}}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>posts/index</title>
</head>
<body>
  <h1>显示结果:</h1> 
  <h2>{{.test}}</h2>
</body>
</html>
{{end}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants