Failure to use JSON on INTEL XDK

0

I'm making an application on INTEL XDK, in which I need to connect to the Database. I created PHP to return with JSON and in Intel, I made the function to return such data. But the application is not returning anything, and since I never did anything in this area, I decided to come here to ask for help. Codes:

JSON.PHP

<?php
    header("Access-Control-Allow-Origin: *");
    header("Content-type: application/json; charset=UTF-8");

    $conecta = pg_connect("host=localhost port=5432 dbname=**** user=****** password=******");
    $sql = "select * from oi";
    $resultado= pg_query($conecta, $sql); 

echo '{"produto":}';
$saida = "[";

while($r = pg_fetch_array($resultado))
{
    if($saida != "["){$saida.= ", ";}
    $saida.= '{"Produto":"'.$r["nomeprod"].'"}';

}
$saida.="}}";
echo $saida;

        ?>

App.js

$(document).ready(function(){
$.ajax({
    type:"GET",
    url:"http://200.145.153.172/bruno_pereira/TCC/json.php",
    dataType:"json"
}).done(function(data){
        var mostra = "";
        $.each(data.produto, function(i,x){
            mostra+= "<li>"+x.Produto+"</li>";
        });
$("#result").html(mostra);        
            });
});
    
asked by anonymous 03.09.2016 / 03:03

1 answer

0

Hello,

Try to change the php to call the json_encode function to do the encoding automatically, so you do not need to mount the JSON manually:

PHP     

$conecta = pg_connect("host=localhost port=5432 dbname=**** user=****** password=******");
$sql = "select * from oi";
$resultado= pg_query($conecta, $sql); 

$r['dados'] = $resultado; 
echo json_encode($resultado);

    ?>

And showing if the request succeeds:

JS

$(document).ready(function(){
$.ajax({
  type:"GET",
  url:"http://200.145.153.172/bruno_pereira/TCC/json.php",
  dataType:"json"
    }).done(function(data){
         var mostra = "";
         for (i=0; i< data.dados.length;i++){
           mostra+= "<li>"+data.dados[i].nome_do_campo_desejado+"</li>";
           /* esse nome do campo desejado deve ser o nome de algum campo desejado que tenha 
              sido pesquisado no SELECT do PHP */
         }



$("#result").html(mostra);        
        });
});
    
07.09.2016 / 04:05