示例#1
0
func (r *JsonResult) Write(ctx framework.WebContext) error {
	data, err := json.Marshal(r.Value)

	if err != nil {
		return err
	}

	ctx.WriteHeader(http.StatusOK)

	callback, _ := ctx.Param("callback")

	if callback == "" {
		//json
		ctx.ContentType("application/json")
		_, err = ctx.Write(data)
	} else {
		//jsonp
		ctx.ContentType("application/javascript")
		_, err = ctx.Write([]byte(fmt.Sprintf(jsonpFormat, callback)))
		_, err = ctx.Write(data)
		_, err = ctx.Write(jsonpEnclosing)
	}

	return err
}
示例#2
0
func (r *TextResult) Write(ctx framework.WebContext) error {

	ctx.WriteHeader(http.StatusOK)
	ctx.ContentType("text/plain")
	_, err := ctx.Write([]byte(r.Value))

	return err
}
示例#3
0
func (r *XmlResult) Write(ctx framework.WebContext) error {
	data, err := xml.Marshal(r.Value)

	if err == nil {
		ctx.WriteHeader(http.StatusOK)
		ctx.ContentType("application/xml")
		ctx.Write(data)
	}

	return err
}