MCPcopy Index your code
hub / github.com/TruthHun/BookStack / JsonResult

Method JsonResult

controllers/BaseController.go:218–249  ·  view source on GitHub ↗

JsonResult 响应 json 结果

(errCode int, errMsg string, data ...interface{})

Source from the content-addressed store, hash-verified

216
217// JsonResult 响应 json 结果
218func (this *BaseController) JsonResult(errCode int, errMsg string, data ...interface{}) {
219 if !this.Ctx.Input.IsAjax() && this.Ctx.Request.Method == "GET" {
220 this.Data["Message"] = errMsg
221 this.Abort("404")
222 }
223
224 jsonData := make(map[string]interface{}, 3)
225 jsonData["errcode"] = errCode
226 jsonData["message"] = errMsg
227
228 if len(data) > 0 && data[0] != nil {
229 jsonData["data"] = data[0]
230 }
231 returnJSON, err := json.Marshal(jsonData)
232 if err != nil {
233 beego.Error(err)
234 }
235 this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf-8")
236 //this.Ctx.ResponseWriter.Header().Set("Cache-Control", "no-cache, no-store")//解决回退出现json的问题
237 //使用gzip原始,json数据会只有原本数据的10分之一左右
238 if strings.Contains(strings.ToLower(this.Ctx.Request.Header.Get("Accept-Encoding")), "gzip") {
239 this.Ctx.ResponseWriter.Header().Set("Content-Encoding", "gzip")
240 //gzip压缩
241 w := gzip.NewWriter(this.Ctx.ResponseWriter)
242 defer w.Close()
243 w.Write(returnJSON)
244 w.Flush()
245 } else {
246 io.WriteString(this.Ctx.ResponseWriter, string(returnJSON))
247 }
248 this.StopRun()
249}
250
251// ExecuteViewPathTemplate 执行指定的模板并返回执行结果.
252func (this *BaseController) ExecuteViewPathTemplate(tplName string, data interface{}) (string, error) {

Callers 15

CrawlMethod · 0.95
SetFollowMethod · 0.95
SignTodayMethod · 0.95
ReadMethod · 0.80
EditMethod · 0.80
CreateMethod · 0.80
CreateMultiMethod · 0.80
UploadMethod · 0.80
RemoveAttachmentMethod · 0.80
DeleteMethod · 0.80
ContentMethod · 0.80
ExportOldMethod · 0.80

Calls 2

stringFunction · 0.85
SetMethod · 0.80

Tested by

no test coverage detected