ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] # json JSON 数据直接输出: ~~~ func (this *AddController) Get() { mystruct := { ... } this.Data["json"] = &mystruct this.ServeJSON() } ~~~ 调用 ServeJSON 之后,会设置`content-type`为`application/json`,然后同时把数据进行 JSON 序列化输出 ~~~ type JSONStruct struct { Code int Msg string } func (this *AdminController) Post(){ mystruct := &JSONStruct{0, "hello"} this.Data["json"] = mystruct this.ServeJSON() } ~~~ # xml XML 数据直接输出: ~~~ func (this *AddController) Get() { mystruct := { ... } this.Data["xml"]=&mystruct this.ServeXML() } ~~~ 调用 ServeXML 之后,会设置`content-type`为`application/xml`,同时数据会进行 XML 序列化输出 # jsonp ~~~ func (this *AddController) Get() { mystruct := { ... } this.Data["jsonp"] = &mystruct this.ServeJSONP() } ~~~ 调用 ServeJSONP 之后,会设置`content-type`为`application/javascript`,然后同时把数据进行 JSON 序列化,然后根据请求的 callback 参数设置 jsonp 输出