新聞中心
相關(guān)方法:

十載的淄川網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整淄川建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“淄川網(wǎng)站設(shè)計”,“淄川網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
func (r *Response) WriteJson(content interface{}) error
func (r *Response) WriteJsonExit(content interface{}) error
func (r *Response) WriteJsonP(content interface{}) error
func (r *Response) WriteJsonPExit(content interface{}) error
func (r *Response) WriteXml(content interface{}, rootTag ...string) error
func (r *Response) WriteXmlExit(content interface{}, rootTag ...string) error?Response?提供了對?JSON/XML?數(shù)據(jù)格式輸出的原生支持,通過以下方法實現(xiàn):
- ?
WriteJson*? 方法用于返回?JSON?數(shù)據(jù)格式,參數(shù)為任意類型,可以為?string?、?map?、?struct?等等。返回的?Content-Type?為?application/json?。 - ?
WriteXml*? 方法用于返回?XML?數(shù)據(jù)格式,參數(shù)為任意類型,可以為?string?、?map?、?struct?等等。返回的?Content-Type?為?application/xml?。
對?JSON?數(shù)據(jù)格式支持的同時,同時也支持?JSONP?協(xié)議。
JSON
package main
import (
"github.com/GOgf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/json", func(r *ghttp.Request) {
r.Response.WriteJson(g.Map{
"id": 1,
"name": "john",
})
})
})
s.SetPort(8199)
s.Run()
}執(zhí)行后,我們通過?curl?工具測試下:
$ curl -i http://127.0.0.1:8199/json
HTTP/1.1 200 OK
Content-Type: application/json
Server: GF HTTP Server
Date: Sun, 05 Jan 2020 02:49:31 GMT
Content-Length: 22
{"id":1,"name":"john"}JSONP
需要注意使用?JSONP?協(xié)議時必須通過?Query?方式提供?callback?參數(shù)。
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/jsonp", func(r *ghttp.Request) {
r.Response.WriteJsonP(g.Map{
"id": 1,
"name": "john",
})
})
})
s.SetPort(8199)
s.Run()
}執(zhí)行后,我們通過?curl?工具測試下:
$ curl -i "http://127.0.0.1:8199/jsonp?callback=MyCallback"
HTTP/1.1 200 OK
Server: GF HTTP Server
Date: Sun, 05 Jan 2020 02:50:42 GMT
Content-Length: 34
Content-Type: text/plain; charset=utf-8
MyCallback({"id":1,"name":"john"})XML
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/xml", func(r *ghttp.Request) {
r.Response.Write(``)
r.Response.WriteXml(g.Map{
"id": 1,
"name": "john",
})
})
})
s.SetPort(8199)
s.Run()
}執(zhí)行后,我們通過?curl?工具測試下:
$ curl -i http://127.0.0.1:8199/xml
HTTP/1.1 200 OK
Content-Type: application/xml
Server: GF HTTP Server
Date: Sun, 05 Jan 2020 03:00:55 GMT
Content-Length: 76
1 john 網(wǎng)站題目:創(chuàng)新互聯(lián)GoFrame教程:GoFrame數(shù)據(jù)返回-JSON/XML
文章URL:http://www.dlmjj.cn/article/dphdghh.html


咨詢
建站咨詢
