1

I have this 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)

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

What i get in the ajax data is

{"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"}}

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

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.

Exemple

for(...){

   innerhtml += <div clas="">

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

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

                </div>

}

2 Answers 2

2

try this one (after callback add type: 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);

Sign up to request clarification or add additional context in comments.

3 Comments

i tried already but whenever i put ex:var obj=jQuery.parseJSON(json_data); but the alert or the consoleLog will not work
use: var obj = JSON.parse(data); so: $$.post('theIP/eatsServer/dailyMenu.php', {}, function (data) { var obj = JSON.parse(data); console.log(obj); }, 'json');
I putted what u said in the commentatys but that isnt in the answer so do i mark you anwser as correct or what?here if u need to edit what i putted $$.post('url', {}, function (data) { var obj = JSON.parse(data); console.log(obj); alert(obj["1"].title); }); NOTE: Thanks it work ;)
1

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

$$.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.

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({ 
        url: "url_here",
        method: "POST",
        dataType:"json",        
        data: {},
        success: function(r){   
          // response r.
        }, error: function(error){
          //error
        }
    }); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.