fix README.md code bug and Change map to gin.H (#1963)

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

        // r.GET("/JSONP?callback=x", func(c *gin.Context) { // old
        r.GET("/JSONP", func(c *gin.Context) {    // new
                data := gin.H{
                        "foo": "bar",
                }

                //callback is x
                // Will output  :   x({\"foo\":\"bar\"})
                c.JSONP(http.StatusOK, data)
        })

        // Listen and serve on 0.0.0.0:8080
        r.Run(":8080")
}

// client
// curl http://127.0.0.1:8080/JSONP?callback=x

// old output
// 404 page not found

// new output
// x({"foo":"bar"})

```

Most of the sample code in the documentation map[string]interface{} is represented by gin.H.
gin.H is a very important place for me to like gin, can write a lot less code
This commit is contained in:
guonaihong 2019-06-28 09:25:19 +08:00 committed by thinkerou
parent f98b339b77
commit 31342fc03f

View File

@ -1119,8 +1119,8 @@ Using JSONP to request data from a server in a different domain. Add callback t
func main() { func main() {
r := gin.Default() r := gin.Default()
r.GET("/JSONP?callback=x", func(c *gin.Context) { r.GET("/JSONP", func(c *gin.Context) {
data := map[string]interface{}{ data := gin.H{
"foo": "bar", "foo": "bar",
} }
@ -1131,6 +1131,9 @@ func main() {
// Listen and serve on 0.0.0.0:8080 // Listen and serve on 0.0.0.0:8080
r.Run(":8080") r.Run(":8080")
// client
// curl http://127.0.0.1:8080/JSONP?callback=x
} }
``` ```
@ -1143,7 +1146,7 @@ func main() {
r := gin.Default() r := gin.Default()
r.GET("/someJSON", func(c *gin.Context) { r.GET("/someJSON", func(c *gin.Context) {
data := map[string]interface{}{ data := gin.H{
"lang": "GO语言", "lang": "GO语言",
"tag": "<br>", "tag": "<br>",
} }
@ -1352,7 +1355,7 @@ func main() {
router.LoadHTMLFiles("./testdata/template/raw.tmpl") router.LoadHTMLFiles("./testdata/template/raw.tmpl")
router.GET("/raw", func(c *gin.Context) { router.GET("/raw", func(c *gin.Context) {
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{ c.HTML(http.StatusOK, "raw.tmpl", gin.H{
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC), "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
}) })
}) })