This is my json file after php encode values:
这是php编码值后的json文件:
/database/database.php
/database/database.php
[{"id":"1","title":"text","text":"ttexte","image":"dsgdsgs","User_id":"1"},{"id":"2","title":"titles","text":"sfsf","image":"safasfa","User_id":"1"}]
This is my Ajax code /js/database.js
这是我的Ajax代码/js/database.js
$.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);
});
}
});
I'm in /blogs.php to retrieve values using call <script src="js/database.js"></script>
我在/blogs.php中使用call
Console log: XHR finished loading: GET "/database/database.php".
控制台日志:XHR完成加载:GET“/database/database.php”。
I have already done debug and don't receive values.
我已经完成了调试并且没有收到值。
1 个解决方案
#1
0
The result returned using AJAX is already JSON parsed, so the JSON.parse is not required and is the reason why it fails.
使用AJAX返回的结果已经过JSON解析,因此不需要JSON.parse,这就是它失败的原因。
Just modify your code to avoid that and you should be fine.
只需修改您的代码以避免这种情况,您应该没问题。
$.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);
});
}
});
#1
0
The result returned using AJAX is already JSON parsed, so the JSON.parse is not required and is the reason why it fails.
使用AJAX返回的结果已经过JSON解析,因此不需要JSON.parse,这就是它失败的原因。
Just modify your code to avoid that and you should be fine.
只需修改您的代码以避免这种情况,您应该没问题。
$.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);
});
}
});