使用 SecureJSON 防止 json 劫持。如果给定的结构是数组值或map,则默认预置 "while(1),"
到响应体。
package main import ( "github.com/gin-gonic/gin" ) func main() { router := gin.Default() // 也可以自定义 SecureJSON 前缀 //router.SecureJsonPrefix("')]}', \n") // 返回值: /* ')]}', ["zhangsan","李四","wangwu"] */ router.GET("some_json", func(c *gin.Context) { names := []string{"zhangsan", "李四", "wangwu"} c.SecureJSON(200, names) }) // 返回值:while(1);["zhangsan","李四","wangwu"] router.Run() }