beego中使用jquery ajax请求后台数据
2019-08-21 本文已影响0人
b77bb494e846
1、控制器
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"yunlaba/crm-web/models"
)
type MapController struct {
beego.Controller
}
func (c *MapController) Get() {
c.TplName = "map.html"
}
func (c *MapController) Update() {
fmt.Println("是否为ajax请求",c.IsAjax())
//c.Data["json"]= `[{"name":"cxh","sex":"man"},{"name":"cxh1","sex":"man1"}]`
c.Data["json"] = models.GetData()
c.ServeJSON()
}
2、路由设置
package routers
import (
"yunlaba/crm-web/controllers"
"github.com/astaxie/beego"
)
func init() {
beego.Router("/", &controllers.MainController{})
beego.Router("/map", &controllers.MapController{})
beego.Router("/map/Update",&controllers.MapController{},"*:Update")
}
3、前端页面调用
function ajaxRequest() {
$.ajax({
url: "{{urlfor "MapController.Update"}}",
async: false,
type: "GET",
dataType: "json",
contentType: "application/json;charset=utf-8",
error: function (xhr) {
alert("错误提示: " + xhr.status + " " + xhr.statusText);
},
success: function (data) {
lations=data;
}
});
// alert(t)
}