[英]send multidimensional array to php using ajax as json and receive html text to show in a div
[英]Receive php multidimensional array in ajax
我有這個PHP
include_once($preUrl . "openDatabase.php");
$sql = 'SELECT * FROM dish';
$query = mysqli_query($con,$sql);
$nRows = mysqli_num_rows($query);
if($nRows > 0){
$dishes = array();
while($row = $query->fetch_assoc()) {
$dishes[] = $row;
}
}else{
$dishes = "cyke";
}
echo json_encode($dishes , JSON_FORCE_OBJECT);
和這個ajax(在framework7中)
myApp.onPageInit('dailyMenu',function() {
$$.post('http://theIP/eatsServer/dailyMenu.php', {}, function (data) {
console.log(data);
});
});
我在ajax數據中得到的是
{ “0”:{ “IDC”: “2”, “標題”: “helloWorld1”, “副標題”: “hellsubWorld”, “價格”: “16.5”, “IMG”: “IMG / testeImg.jpg”, “索多特”: “0”}, “1”:{ “IDC”: “3”, “標題”: “helloworld2”, “副標題”: “hellosubWorld2”, “價格”: “20.5”, “IMG”: “IMG / testeImg.jpg”, “索多特”: “1”}}
我已經嘗試了數據。[0]; 數據[0]。 data.0; data0當我使用數據[“0”]時只給我'{'。
我想獲取標題和其余內部的那個0.做一個cicle我將打印多個div,我只在html頁面中更改數組位置。
為例
for(...){
innerhtml += <div clas="">
<div class""> data(position i).title </div>
<div> data(position i) subtitle</div>
</div>
}
試試這個(回調后添加類型:json)
$$.post('url', {}, function (data) {
var obj = JSON.parse(data);
console.log(obj);
alert(obj["1"].title);
});
或者你可以使用JSON.parse(數據);
由於您收到json數據作為響應,您應該使用:
$$.post('http://theIP/eatsServer/dailyMenu.php', {}, function (data) {
console.dir(data);
},'json');
注意},'json');
在代碼結束時,現在$$。post正在以JSON的形式讀取響應。
如果您沒有對數據庫進行任何更新,可以使用:
$$.getJSON('http://theIP/eatsServer/dailyMenu.php',{}, function (data) {
console.dir(data);
});
這是使用$$。ajax的方式:
$$.ajax({
url: "url_here",
method: "POST",
dataType:"json",
data: {},
success: function(r){
// response r.
}, error: function(error){
//error
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.