I need the application to communicate with a server via JSON to log in. How to do?
Note: Just a simple communication via JSON using Cordova, where the answer was very enlightening.
I need the application to communicate with a server via JSON to log in. How to do?
Note: Just a simple communication via JSON using Cordova, where the answer was very enlightening.
For you to communicate with a server, you must already have your back end structured, after that, just make the request. An example, using jQuery
and PHP
Back.
$.ajax({
type: 'POST',
url: 'URLDOMEUARQUIVO.php',
data: {
email: $('#email').val(),
senha: $('#senha').val()
},
dataType: 'json',
beforeSend: function(){
alert('Carrengado...')
},
success: function(data){
localStorage.setItem('id', data.id)
}
})
With the code above, you are sending a request to your php
file requesting the information, in case the user exists, it will return a json
with the data.
<?php
$retorno = array();
$sql = $pdo->prepare("SELECT * FROM users WHERE email = ? AND senha = ?");
$sql->execute(array($_POST['email'], $_POST['senha']));
if($sql->rowCount() > 0){
$retorno['status'] = 1
$retorno['dados'] = $sql->fetchAll();
}
$retorno['status'] = 0
die(json_encode($retorno));
And the code above would be more or less the behavior of back
, of course this is just an example and should be adapted as you wish. If you have any difficulty, I encourage you to check out this video