gin
gin copied to clipboard
redirect is not success
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?
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}}