public ActionResult Index()
{
//创建一个URL类
Uri uri = new Uri("http://localhost:65517/");
//创建一个HttpClient客户端对象
HttpClient httpclient = new HttpClient();
//设置HttpClient要获取资源的地址
httpclient.BaseAddress =uri;
//发送一个Get方法的请求
var GetAsync= httpclient.GetAsync("api/GLB");
//获取Get方法取到数据后的属性 :Result
var typeGetAsync = GetAsync.Result;
//判断返回数据的状态
if (typeGetAsync.StatusCode == System.Net.HttpStatusCode.OK)
{
//定义变量接收结果值
//Content:获取HTTP响应消息的内容
//ReadAsStringAsync():以异步操作将HTTP内容写入流
//Result:获取结果值
var result = typeGetAsync.Content.ReadAsStringAsync().Result;
//转换数据格式
var List = JsonConvert.DeserializeObject<List<tb_personal>>(result).ToList();
//释放资源
httpclient.Dispose();
////return View(List);
//var tolist = Json(List, JsonRequestBehavior.AllowGet)
// ;
var toresult = new { code = 0, msg = "", data = List
};
return Json(toresult,JsonRequestBehavior.AllowGet);
}
else {
ViewBag.ErrMsg = "获取数据失败,错误代码" + Response.StatusCode;
//释放资源
httpclient.Dispose();
}
return View();
}
<script>
layui.use('demo', function () {
var table = layui.table;
//第一个实例
table.render({
elem: '#demo'
, height: 500
, url: '/GLB/' //数据接口
//, page: true //开启分页
, method: 'get'
, cols: [[ //表头
{ field: 'P_PID', title: '用户编号', width: 80, sort: true},
{ field: 'P_PID', title: '用户编号', width: 80, sort: true, fixed: 'left' },
{ field: 'P_Name', title: '姓名', width: 80 },
{ field: 'P_Sex', title: '性别', width: 80, sort: true },
{ field: 'P_Age', title: '年龄', width: 80 },
{ field: 'P_Phone', title: '手机号', width: 177 }
]]
});
});
</script>