Когда я повторяю в своем php-коде json_encode($state);
, в консоли будут отображаться данные в этом формате
{"branchid":"71","branchareaid":"4","branchname":"Zaidy","branchno":"11","branchnamearabic":"\u0625\u064a\u0645\u064a \u0632\u064a\u062f\u064a","branchaddress":"Unnamed Road, Al Hamra and Umm Al Jud, Makkah 24331, Saudi Arabia","branchlat":"21.38958330","branchlong":"39.70306130","branchstatus":"1","mobileno":"0566689175","googlemaplink":"https:\/\/goo.gl\/maps\/rK3oKf1wfVu","workingHours":"6 am to 3 am"}
У объекта JSON есть обе фигурные скобки с разделителями запятыми, когда я хочу напечатать в моем ajaxкод через цикл выдает ошибку и не печатает значения в li
<?php
include "admin/includes/dbconnection.php";
if($_POST){
$state = $_POST['state'];
}else{
$state = 1;
}
?>
<?php
$query = "SELECT * FROM branches WHERE branchareaid = {$state}";
$res = mysqli_query($conn,$query);
while($state = mysqli_fetch_assoc($res)){
echo json_encode($state);
}
?>
Это мой ajax-код, который выбирает запись, но цикл не печатает значения объекта JSON
$(document).ready(function(){
var url = "ajax-states.php";
$("#state").change(function(){
$.ajax({
type: "POST",
data: {state: $('#state').val()},
url: url,
success: function(result){
$("#result").html(result);
console.log(result);
//var c=0;
$.each( result, function( index, branch) {
//var state = states[c];
$('.locations').append("<li>"+branch.branchname+"</li>");
c++;
});
}
});
});
});
jquery.min.js:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in
{"branchid":"71","branchareaid":"4","branchname":"Zaidy","branchno":"11","branchnamearabic":"\u0625\u064a\u0645\u064a \u0632\u064a\u062f\u064a","branchaddress":"Unnamed Road, Al Hamra and Umm Al Jud, Makkah 24331, Saudi Arabia","branchlat":"21.38958330","branchlong":"39.70306130","branchstatus":"1","mobileno":"0566689175","googlemaplink":"https:\/\/goo.gl\/maps\/rK3oKf1wfVu","workingHours":"6 am to 3 am"}
at C (jquery.min.js:2)
at Function.each (jquery.min.js:2)
at Object.success (new-find-a-store.php:553)
at u (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at k (jquery.min.js:2)
at XMLHttpRequest.<anonymous> (jquery.min.js:2)