jQuery怎么解析ajax返回的json数据,里面包含一个数组
下面是前段代码:success:function(msg){if(msg.err_code==0){varstr="";varpage="";varjson=eval(m...
下面是前段代码:
success: function (msg) {
if (msg.err_code==0) {
var str = "";
var page="";
var json = eval(msg.return_data); //数组
$.each(json, function (index) {
//循环获取数据
str += "<tr>" +
"<td>" + json[index].usergameid + "</td>" +
"<td>" + json[index].agentid + "</td>" +
"<td>" + json[index].score + "</td>" +
"<td>" + json[index].date+ "</td>" +
"</tr>";
page+="<a href=../GetDialRecord.asp?gameid"+gameid+"&starttime="+"&endtime="+"&page="+index+">"+index+"</a>";
});
tbody.innerHTML = str;
}
},
下面是json数据
{"err_code":0,"err_msg":"操作成功!","return_data":[{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/6 15:06:31"},{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/6 17:10:27"},{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/6 22:25:24"}],"num":26} 展开
success: function (msg) {
if (msg.err_code==0) {
var str = "";
var page="";
var json = eval(msg.return_data); //数组
$.each(json, function (index) {
//循环获取数据
str += "<tr>" +
"<td>" + json[index].usergameid + "</td>" +
"<td>" + json[index].agentid + "</td>" +
"<td>" + json[index].score + "</td>" +
"<td>" + json[index].date+ "</td>" +
"</tr>";
page+="<a href=../GetDialRecord.asp?gameid"+gameid+"&starttime="+"&endtime="+"&page="+index+">"+index+"</a>";
});
tbody.innerHTML = str;
}
},
下面是json数据
{"err_code":0,"err_msg":"操作成功!","return_data":[{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/6 15:06:31"},{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/6 17:10:27"},{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/6 22:25:24"}],"num":26} 展开
1个回答
展开全部
var json = '{"err_code":0,"err_msg":"操作成功!","return_data":[{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/6 15:06:31"},{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/6 17:10:27"},{"usergameid":"10048","agentid":"10057","score":"1","date":"2017/9/6 22:25:24"}],"num":26}';
json = $.parseJSON(json);
if( !json.err_code ){
$('body').append('<p style="color: blue;">'+json.err_msg+'</p><br/><p>数据如下:</p><br/><table id="data-table"><thead><tr><td>agentid</td><td>date</td><td>score</td><td>usergameid</td></tr></thead><tbody></tbody></table>');
var data = json.return_data;
if( data ){
if( $.isArray(data) ){
for( var i in data ){
$('#data-table').children('tbody').append('<tr><td>'+data[i].agentid+'</td><td>'+data[i].date+'</td><td>'+data[i].score+'</td><td>'+data[i].usergameid+'</td></tr>');
}
}
}
}
更多追问追答
追问
大神您好,您的答案发现在执行到json = $.parseJSON(json);时不往下执行了,jQuery版本是jquery-1.10.2.js,这是什么原因。$.ajax({success: function (msg) { alert(msg.err_msg); var json = $.parseJSON(msg);alert(json.err_msg);第二个没有执行
追答
是我的代码没有往下执行还是你经过整合后没有往下执行
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询