0

Hi i want make simple call data with some ajax and json

i have json data in this url : http://webqu.hostoi.com/webqu/php/data.php

this is my php code

$sql = "SELECT * FROM kasir";
$hasil = mysqli_query($mysqli, $sql);
$results = array();

foreach($hasil as $row){
     $results[] = array(          
          'ID_Kasir'=> $row[ID_Kasir],
          'Kasir_username'=> $row[Kasir_Username],
          'Password'=> $row[Password],
          'Nama'=> $row[Nama],
          'ID_Jenis_kelamin'=> $row[ID_Jenis_kelamin],          
     );
}
$json = json_encode($results, JSON_PRETTY_PRINT);
header('Content-type: application/json');
//header('Content-disposition: attachment; filename=kasir.json');
echo $json;

and i call it with some ajax function like this

$.ajax({            
  url         : "http://webqu.hostoi.com/webqu/php/data.php",
  dataType    : "json",
  success     : function(data){}
      }); 

but i cant get data from that, i use it with dataType : jsonp too

i can get data if running in localhost

i hope u can help me Thx :D

1 Answers1

0

One issue you'll be experiencing is the fact you can't iterate through the mysqli response resource using foreach(), you need to convert the resource to an array first. Like this:

$sql = "SELECT * FROM kasir";
$hasil = mysqli_query($mysqli, $sql);
$results = array();

while($row = mysqli_fetch_assoc($hasil)){
    // ...
}
Enstage
  • 2,106
  • 13
  • 20