在ajax中接收php多维数组。

时间:2022-11-16 21:31:12

I have this php

我有一个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);

and this ajax (in framework7)

这个ajax(在framework7中)

myApp.onPageInit('dailyMenu',function() {
    $$.post('http://theIP/eatsServer/dailyMenu.php', {}, function (data) {
            console.log(data);
    });
});

What i get in the ajax data is

我从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 ":{“idC”:“2”、“标题”:“helloWorld1”、“副标题”:“hellsubWorld”、“价格”:“16.5”,“img”:“img / testeImg.jpg”、“soldout”:“0”}," 1 ":{“idC”:“3”,“标题”:“helloworld2”、“副标题”:“hellosubWorld2”、“价格”:“20.5”,“img”:“img / testeImg.jpg”、“soldout”:" 1 " } }

I already tried data.[0]; data.['0']; data.0; data0 when i use data["0"] just gives me the '{'.

我已经试过数据。[0];数据。(“0”);data.0;当我使用data["0"]时,data0只给我一个'{'

I want to acess the title and the rest inside that 0. to do a cicle for where i will print multiple divs where i only change the array position in a html page.

我想在0里面加上标题。要做一个cicle,我将在那里打印多个div,在那里我只改变html页面中的数组位置。

Exemple

为例

for(...){

   innerhtml += <div clas="">

                  <div class""> data(position i).title </div>

                  <div> data(position i) subtitle</div>

                </div>

}

2 个解决方案

#1


2  

try this one (after callback add type: json)

尝试这个(在回调后添加类型:json)

$$.post('url', {}, function (data) { 
  var obj = JSON.parse(data); 
  console.log(obj); 
  alert(obj["1"].title); 
});

or maybe you can use JSON.parse(data);

或者可以使用JSON.parse(data);

#2


1  

Since you are receiving a json data as response, you should use this:

由于您正在接收json数据作为响应,您应该使用以下内容:

$$.post('http://theIP/eatsServer/dailyMenu.php', {}, function (data) {
        console.dir(data);
},'json');

Pay attention to },'json');on end of the code, now the $$.post is reading the response as a JSON.

注意},“json”);在代码的末尾,现在是$$。post将响应读取为JSON。

If you aren't doing any update to data base, you could use:

如果您没有对数据库进行任何更新,您可以使用:

$$.getJSON('http://theIP/eatsServer/dailyMenu.php',{}, function (data) {
  console.dir(data);
}); 

This is the way with $$.ajax:

这是用$.ajax的方式:

    $$.ajax({ 
        url: "url_here",
        method: "POST",
        dataType:"json",        
        data: {},
        success: function(r){   
          // response r.
        }, error: function(error){
          //error
        }
    }); 

#1


2  

try this one (after callback add type: json)

尝试这个(在回调后添加类型:json)

$$.post('url', {}, function (data) { 
  var obj = JSON.parse(data); 
  console.log(obj); 
  alert(obj["1"].title); 
});

or maybe you can use JSON.parse(data);

或者可以使用JSON.parse(data);

#2


1  

Since you are receiving a json data as response, you should use this:

由于您正在接收json数据作为响应,您应该使用以下内容:

$$.post('http://theIP/eatsServer/dailyMenu.php', {}, function (data) {
        console.dir(data);
},'json');

Pay attention to },'json');on end of the code, now the $$.post is reading the response as a JSON.

注意},“json”);在代码的末尾,现在是$$。post将响应读取为JSON。

If you aren't doing any update to data base, you could use:

如果您没有对数据库进行任何更新,您可以使用:

$$.getJSON('http://theIP/eatsServer/dailyMenu.php',{}, function (data) {
  console.dir(data);
}); 

This is the way with $$.ajax:

这是用$.ajax的方式:

    $$.ajax({ 
        url: "url_here",
        method: "POST",
        dataType:"json",        
        data: {},
        success: function(r){   
          // response r.
        }, error: function(error){
          //error
        }
    });