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