Request problem - Phonegap + Ajax

Asked

Viewed 383 times

1

was doing a test to test the phonegap, but I’m not able to make an Ajax request in the app, but the request works in the browser.

$( "#botaoBusca" ).click(function(){

var cep = $( "#cep" ).val();

$.getJSON('https://viacep.com.br/ws/'+cep+'/json/?callback=?', function(data) {

var endereco =
   { rua    : data.logradouro,
     cidade : data.localidade,
     uf     : data.uf
   };

$( "#endereco" ).text("Você mora na "+endereco.rua+", "+endereco.cidade+" - "+endereco.uf);

});
});

HTML:

</head>
 <body>
   <div class="container text-center">
   <div class="row">
    <h1>Olá, AJAX</h1>

            <label>Digite o seu cep:</label>   
            <input id="cep" type="text" pattern="\d{8}" maxlength="8"><br>
            <button class="btn btn-primary" id="botaoBusca" type="button">Buscar</button>    

            <h3 id="endereco"></h3>
        </div>
    </div>    
    <script src="js/jquery-1.12.2.min.js"></script>
    <script src="js/jquery.mobile-1.4.5.min.js"></script>
    <script src="js/ajax.js"></script>
</body>

Thank you

  • By default, Phonegap has a "block" to access web pages by the app. But for those who need this access, he has in his own documentation a guide explaining how to solve it. Follow the link: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/#supported-Cordova-Platforms

1 answer

-1

You put the permission for the application to use internet ? If you have not put android does not let the application use internet resources, so your code will not communicate with the server.

And if you are using Cordova 4.1+ ( or higher ) could be a problem with Whitelist, check there to see if it is installed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.