ajax获得后台传来的一个json值,在js中获得其中的属性值

时间:2023-03-09 00:49:51
ajax获得后台传来的一个json值,在js中获得其中的属性值

首先

ajax的dataType需要设置为json,

默认的text获取属性值在jquery3.2.1中尝试不成功

获得属性值的方式:

类似数组,键值对的方式

下面例子:

设置dataType为json

//页面加载完之后,直接去发送一个ajax请求,获取分页数据
$(function () {
$.ajax({
url:'${APP_PATH}/emps'
,data:'pn=1'
,type:'get'
,dataType:'json'
,success:function (result) {
console.log(result);
//解析json,显示员工数据
build_emps_table(result);
//解析json,显示分页导航信息
build_page_nax(result);
}
});
});

获取属性值:

//构建员工表格
function build_emps_table(result) {
//清空table表格
$("#emps_table tbody").empty();
console.log(result['extend']['pageInfo']['list']);
var emps = result['extend']['pageInfo']['list'];
$.each(emps,function (index,item) {
alert(item.empName);
})
}

成功:

ajax获得后台传来的一个json值,在js中获得其中的属性值