jquery - How to pass values from json_encode using PHP to Ajax -
this json file after php encode values:
/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 ajax code /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 retrieve values using call <script src="js/database.js"></script>
console log: xhr finished loading: get "/database/database.php".
i have done debug , don't receive values.
the result returned using ajax json parsed, json.parse not required , reason why fails.
just modify code avoid , should 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); }); } });
Comments
Post a Comment