在 ajax 中接收 php 多维数组
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","title":"helloWorld1","subtitle":"hellsubWorld","price" :"16.5","img":"img/testeImg.jpg","soldout":"0"},"1":{"idC":"3","title":"helloworld2","subtitle":"hellosubWorld2","price":"20.5","img":"img/testeImg.jpg","soldout":"1"}}
我已经试过数据了[0];数据.['0'];数据.0;当我使用 data["0"] 时,data0 只给我 '{'.
我想访问标题和 0 中的其余部分。为我将在其中打印多个 div 的位置做一个 cicle,我只更改 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(data);
由于您收到 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
}
});
我有这个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","title":"helloWorld1","subtitle":"hellsubWorld","price" :"16.5","img":"img/testeImg.jpg","soldout":"0"},"1":{"idC":"3","title":"helloworld2","subtitle":"hellosubWorld2","price":"20.5","img":"img/testeImg.jpg","soldout":"1"}}
我已经试过数据了[0];数据.['0'];数据.0;当我使用 data["0"] 时,data0 只给我 '{'.
我想访问标题和 0 中的其余部分。为我将在其中打印多个 div 的位置做一个 cicle,我只更改 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(data);
由于您收到 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
}
});