本文整理汇总了Golang中github.com/astaxie/beego.Controller.ServeJson方法的典型用法代码示例。如果您正苦于以下问题:Golang Controller.ServeJson方法的具体用法?Golang Controller.ServeJson怎么用?Golang Controller.ServeJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/astaxie/beego.Controller
的用法示例。
在下文中一共展示了Controller.ServeJson方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CheckAdminLogin
//判断是否登陆
func CheckAdminLogin(c *beego.Controller, t int64) {
id, _ := c.GetSession("adminid").(int)
username, _ := c.GetSession("adminname").(string)
adminrole, _ := c.GetSession("adminrole").(int)
if id == 0 || username == "" || adminrole == 0 {
switch t {
case 0:
c.Redirect("/admin/login", 301)
case 1:
//返回JSON
json := &models.TipJSON{}
json.Status = models.TipError
json.Message = "请先登录,再执行此操作"
json.ReturnUrl = "/admin/login"
c.Data["json"] = json
c.ServeJson()
c.StopRun()
}
}
c.Data["AdminName"] = username
c.Data["AdminId"] = id
c.Layout = "admin/layout.tpl"
}
示例2: EchoTip
//信息提示,如果异步,返回json、如果同步,则页面提示并返回
func EchoTip(c *beego.Controller, json *models.TipJSON) {
if c.IsAjax() {
//异步提交
c.Data["json"] = json
c.ServeJson()
c.StopRun()
} else {
tpl := "admin/error.tpl"
if json.Status == models.TipSuccess {
tpl = "admin/success.tpl"
}
c.Data["Tip"] = json
c.Layout = tpl
//此处不能用 c.StopRun() 返回,否则会空白页面!
//c.StopRun()
return
}
}
示例3: Restful
//.........这里部分代码省略.........
return true, "" // 插入成功
case "delete": //删
if _ok, _data := this.BaseSetId(contro.GetString("_id")); !_ok {
return false, _data
}
err := this.BaseDelete()
if err != nil {
return false, err
}
return true, nil
case "update": //改
//根据id查询对象
if _ok, _data := this.BaseFind(contro.GetString("_id")); !_ok {
return false, _data
}
//解析请求参数->对象
if err := contro.ParseForm(this); err != nil {
return false, err.Error()
}
// 额外解析数组参数
if _ok, _data := this.BaseFormStrings(contro); !_ok {
return false, _data
}
//验证
valid := validation.Validation{}
b, err := valid.Valid(this)
if err != nil {
return false, "验证参数解析失败"
}
if !b { //验证失败
for _, err := range valid.Errors {
return false, err.Key + " " + err.Message
}
}
if err := this.BaseUpdate(); err != nil {
return false, err.Error()
}
return true, nil
case "get": //查
from, _ := contro.GetInt("from")
number, _ := contro.GetInt("number")
if number > 100 {
return false, "最多查询100条"
}
if contro.GetString("like") != "" && contro.GetString("likeKey") != "" { // 模糊搜索
var result []BaseInterface
var count int
if contro.GetString("likeMethod") == "like" {
result = this.BaseSelectLike(from, number, contro.GetString("sort"), contro.GetString("likeKey"), contro.GetString("like"))
count = this.BaseLikeCount(contro.GetString("likeKey"), contro.GetString("like"))
} else if contro.GetString("likeMethod") == "accuracy" {
result = this.BaseSelectAccuracy(from, number, contro.GetString("sort"), contro.GetString("likeKey"), contro.GetString("like"))
count = this.BaseAccuracyCount(contro.GetString("likeKey"), contro.GetString("like"))
}
return true, map[string]interface{}{
"lists": result,
"count": count,
}
} else { // 普通查询
if contro.GetString("filter") != "" {
filter, _ := contro.GetInt("filter")
count := this.BaseFilterCount(contro.GetString("filterKey"), filter)
result := this.BaseFilterSelect(from, number, contro.GetString("sort"), contro.GetString("filterKey"), filter)
return true, map[string]interface{}{
"lists": result,
"count": count,
}
} else {
count := this.BaseCount()
result := this.BaseSelect(from, number, contro.GetString("sort"))
return true, map[string]interface{}{
"lists": result,
"count": count,
}
}
}
}
return true, nil
}()
contro.Data["json"] = map[string]interface{}{
"ok": ok,
"data": data,
}
contro.ServeJson()
}