这是php编码值后的json文件:
/database/database.php
var aTimer = new Timer();
这是我的Ajax代码 /js/database.js
[{"id":"1","title":"text","text":"ttexte","image":"dsgdsgs","User_id":"1"},{"id":"2","title":"titles","text":"sfsf","image":"safasfa","User_id":"1"}]
我在/blogs.php中使用调用$.ajax({
url: "/database/database.php",
type: "GET",
dataType: 'json',
crossDomain: "true",
success: function(result) {
if (result.type == false) {
alert("Error occured:" + result.data);
return false;
}
$.each(JSON.parse(result.data), function(index, obj) {
$("#get-all").append(
"<div class='span12'><div class='row'><div class='span8'><h4><strong><a href='#'>" + obj.title + "</div></a></strong></h4> </div></div>" +
"<div class='row'><div class='span2'><a href='#' class='thumbnail'><img src='" + obj.id + "' alt=''></a></div><div class='span10'>" +
"<p>" + obj.text + " </div></p></div>")
console.log(obj.text);
});
}
});
控制台日志:XHR已完成加载:<script src="js/database.js"></script>
我已经完成了调试,但没有收到值。
答案 0 :(得分:0)
使用AJAX返回的结果已经过JSON解析,因此不需要JSON.parse,这就是它失败的原因。
只需修改您的代码以避免这种情况,您应该没事。
$.ajax({
url: "/database/database.php",
type: "GET",
dataType: 'json',
success: function(result) {
if (result.type == false) {
alert("Error occured:" + result);
return false;
}
$.each(result, function(index, obj) {
$("#get-all").append(
"<div class='span12'><div class='row'><div class='span8'><h4><strong><a href='#'>" + obj.title + "</div></a></strong></h4> </div></div>" +
"<div class='row'><div class='span2'><a href='#' class='thumbnail'><img src='" + obj.id + "' alt=''></a></div><div class='span10'>" +
"<p>" + obj.text + " </div></p></div>")
console.log(obj.text);
});
}
});