n.parseJSON @ jquery-1.12.3.min.js:4
showTable @ query.html:107
success @ query.html:98
i @ jquery-1.12.3.min.js:2
fireWith @ jquery-1.12.3.min.js:2
y @ jquery-1.12.3.min.js:4
c @ jquery-1.12.3.min.js:4
Above is the error info of code.
I want to parse JSON object to array,and render the array into the HTML. But I don't know how to handle it by the following?
Following is the part js code of mine , function of showTable(data) is parse the json object into html table.function requestData() is request info from the back.
(function(){
var form = $("form");
var contentCenter = $(".content-center");
$(".s-btn").on("click",function(event){
$(".container").css({
"position":"relative"
})
form.css({
"position":"absolute",
"left":"15px",
"top":"0px"
});
contentCenter.css({
"position":"absolute",
"top":"-12px"
})
event.preventDefault();
requestData();
});
//与后台交互数据
function requestData(){
var data = {
type : $("#form_control").val(),
keywords : $.trim($("#ipt_control").val())
};
$.ajax({
type : "GET",
url : "data/data.json",
dataType : "json",
data : data,
success:function(msg){
//TODO请求成功后跳转页面
//处理后台返回的数据
console.log(msg);
showTable(msg);
},
error:function(msg){
console.log("failed");
}
});
}
//获取json数据,动态创建表格
function showTable(data){
var dataArray = $.parseJSON(data);
console.log(dataArray);
var tableStr="<table class='table table-bordered'>"
tableStr = tableStr + "<thead><td>id</td><td>name</td>handle<td></td>"
var len = dataArray.length;
for(var i=0;i<len;i++){
tableStr = tableStr + "<tr><td>" + dataArray[i].id + "</td>" + "<td>" + dataArray[i].name + "</td>" + "<td>" + dataArray[i].handle + "</td></tr>";
}
tableStr = tableStr + "</table>"
$("#dataType").html(tableStr);
}
})();
I had this same problem, if your response header is application/json, it is already parsed, you don't need to parse it with:
var dataArray = $.parseJSON(data);
Also, you can get it with jQuery.getJSON()
: jQuery.getJSON()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With