package routers
import (
"monitor/controllers"
"github.com/astaxie/beego"
//"monitor/http"
)
func init() {
//beego.Router("/", &controllers.MonitorController{})
//beego.Router("/TestServlet", &controllers.MonitorController{}, "POST:RegisterInfo")
//http.CfgKernelRouter()
//http.CfgSystemRouter()
//http.CfgCpuRouter()
//http.CfgMemRouter()
//http.CfgDfRouter()
//http.CfgNetRouter()
//http.CfgIORouter()
beego.Router("/", &controllers.MainController{})
beego.Router("/TestServlet", &controllers.MainController{}, "POST:RegisterInfo")
}
package controllers
import (
"github.com/astaxie/beego"
"monitor/models"
"math/rand"
"fmt"
)
type MonitorController struct {
beego.Controller
}
func (c *MonitorController) Get() {
//c.Data["CpuData"] = models.GetCPUData()
c.Data["Total"] = rand.Int31n(100)
c.Data["FreeNum"] = rand.Int31n(200)
c.Data["UsedNum"] = rand.Int31n(200)
asd := models.MemInfo{123,313,22,}
c.Data["Memdata"] = asd
c.TplName = "monitor.html"
}
func (c *MonitorController) RegisterInfo() {
c.Data["json"] = map[string]interface{}{"name": 222, "num": 1111}
c.ServeJSON()
return
}
$.ajax({
type : "post",
async : true, //异步请求(同步请求将会锁住浏览器,用户其他操作必须等待请求完成才可以执行)
url : "TestServlet", //请求发送到TestServlet处
data : {},
dataType : "json", //返回数据形式为json
success : function(result) {
console.log(result.num)
number = result.num
console.log(number)
},
error : function(errorMsg) {
//请求失败时执行该函数
alert("图表请求数据失败!");
myChart.hideLoading();
}
});
也可以通过下面这种方式实现,可以传递数组格式的json数据
func (c *MainController) RegisterInfo() {
c.Data["json"]= `[{"name":"cxh","sex":"man"},{"name":"cxh1","sex":"man1"}]`
c.ServeJSON()
}
type : "post",
async : true, //异步请求(同步请求将会锁住浏览器,用户其他操作必须等待请求完成才可以执行)
url : "TestServlet", //请求发送到TestServlet处
data : {},
dataType : "json", //返回数据形式为json
success : function(result) {
console.log("11111111111111111111111");
console.log(result);
var obj = jQuery.parseJSON(result);
alert(obj[0].name)
console.log("2222222222222222222")
},
error : function(errorMsg) {
//请求失败时执行该函数
alert("图表请求数据失败!");
myChart.hideLoading();
}
})