JSON communication between domains with Cordova

Asked

Viewed 183 times

-1

I need the application to communicate with a server via JSON to log in. How to do?


Obs: Just a simple communication via JSON using Cordova, where the answer was very enlightening.

  • Do you know anything about the technology you intend to use? Do you have any code with anything you have ever done?

  • It has already been answered. Thank you very much!!

1 answer

0


For you to communicate with a server, you must already have your structured back end, 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 file php requesting the information, if the user exists, he will return you 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 above code would be more or less the behavior of back, of course this is just an example and should be adapted according to your will. If you have any difficulty, I advise you to see this video

Browser other questions tagged

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