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

十載的淄川網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷型網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整淄川建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“淄川網(wǎng)站設(shè)計(jì)”,“淄川網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(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?提供了對(duì)?JSON/XML?數(shù)據(jù)格式輸出的原生支持,通過(guò)以下方法實(shí)現(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?。
對(duì)?JSON?數(shù)據(jù)格式支持的同時(shí),同時(shí)也支持?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í)行后,我們通過(guò)?curl?工具測(cè)試下:
$ 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é)議時(shí)必須通過(guò)?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í)行后,我們通過(guò)?curl?工具測(cè)試下:
$ 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í)行后,我們通過(guò)?curl?工具測(cè)試下:
$ 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
轉(zhuǎn)載來(lái)源:http://m.fisionsoft.com.cn/article/dphdghh.html


咨詢
建站咨詢
