0

I'm a beginner. I've been trying to develop an app using cordova/phonegap. I've made an example database and uploaded it on a free host, and did the same with the php file. I can retrieve data from the DB when on web/desktop, but it doesn't work on the emulator. What should I do?

I've tried to discover what's happening: what I know so far is that the javascript file is ok, since a function of "alert" on it is working. I know my php files aren't working on phonegap because i've tried to update my db and it didn't work.

getDados.php (on a web host):

<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
    //Conectando ao banco de dados
    $con = new mysqli("sql10.freemysqlhosting.net", "login_bd", "senha_bd", "nome_bd");
    if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

    //Consultando banco de dados
    $qryLista = mysqli_query($con, "SELECT * FROM usuarios");    
    while($resultado = mysqli_fetch_assoc($qryLista)){
        $vetor[] = array_map('utf8_encode', $resultado); 
    }    

    //Passando vetor em forma de json
    echo json_encode($vetor);
?>

index.html (on my local machine):

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="utf-8">
        <title>AJAX, JSON E PHP</title>
        <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
        <script src="http://IP DA MÁQUINA LOCAL/geek_phonegap/www/ajax.js"></script>
    </head>
    <body>
        <table border="1" width="500">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Usuario</th>
                    <th>Senha</th>
                </tr>
            </thead>
            <tbody id="tabela"></tbody>
        </table>
        <button onclick="funcao1()">Alert</button>
    </body>
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
</html>

ajax.js (local machine):

function funcao1()
{
    alert("Está chamando o arquivo ajax.js!");
}

$(document).ready(function(){
    $('#tabela').empty(); //Limpando a tabela
    $.ajax({
        type:'post',        //Definimos o método HTTP usado
        dataType: 'json',   //Definimos o tipo de retorno
        url: 'http://uniapp.orgfree.com/getDados.php',//Definindo o arquivo onde serão buscados os dados
        success: function(dados){
            for(var i=0;dados.length>i;i++){
                //Adicionando registros retornados na tabela
                $('#tabela').append('<tr><td>'+dados[i].id+'</td><td>'+dados[i].usuario+'</td><td>'+dados[i].senha+'</td></tr>');
            }
        }
    });
});
Kirankumar Dafda
  • 2,354
  • 5
  • 29
  • 56

0 Answers0