W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
beego 當(dāng)初設(shè)計(jì)的時(shí)候就考慮了 API 功能的設(shè)計(jì),而我們?cè)谠O(shè)計(jì) API 的時(shí)候經(jīng)常是輸出 JSON 或者 XML 數(shù)據(jù),那么 beego 提供了這樣的方式直接輸出:
注意 struct 屬性應(yīng)該 為 exported Identifier 首字母應(yīng)該大寫
func (this *AddController) Get() {
mystruct := { ... }
this.Data["json"] = &mystruct
this.ServeJSON()
}
調(diào)用 ServeJSON 之后,會(huì)設(shè)置 content-type 為 application/json,然后同時(shí)把數(shù)據(jù)進(jìn)行 JSON 序列化輸出。 func (this *AddController) Get() {
mystruct := { ... }
this.Data["xml"]=&mystruct
this.ServeXML()
}
調(diào)用 ServeXML 之后,會(huì)設(shè)置 content-type 為 application/xml,同時(shí)數(shù)據(jù)會(huì)進(jìn)行 XML 序列化輸出。 func (this *AddController) Get() {
mystruct := { ... }
this.Data["jsonp"] = &mystruct
this.ServeJSONP()
}
調(diào)用 ServeJSONP 之后,會(huì)設(shè)置 content-type 為 application/javascript,然后同時(shí)把數(shù)據(jù)進(jìn)行 JSON 序列化,然后根據(jù)請(qǐng)求的 callback 參數(shù)設(shè)置 jsonp 輸出。開發(fā)模式下序列化后輸出的是格式化易閱讀的 JSON 或 XML 字符串;在生產(chǎn)模式下序列化后輸出的是壓縮的字符串。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: