Send PhoneGap data to Web Service

0

I'm creating an app with Phonegap and I do not know how to send a string from my .js to a web service that I have created with Java, some suggestions I'm new to this and I do not find anything clear, thanks. My JSON function is as follows

myfuncion: function() {
  var x = document.getElementById("login").value;
  document.getElementById("demo").innerHTML = x;     
}

I could do a post with Ajax from here like this:

 $.ajax({
  type: "POST",
  url: "http://localhost:8080/HelloSpringMVC/hello",
  data: {x} ,
  )}; 

I do not understand if you can use $ .ajax like this and pass the variable x.

    
asked by Silvia 10.03.2018 в 14:55
source

1 answer

1

First: I made a small change in the function myfuncion to return the value of x.

function myfuncion() {
  var x = document.getElementById("login").value;
  document.getElementById("demo").innerHTML = "El texto demo es : " + x;
  return x;
}

$('#btn').click(function() {

  $.ajax({
    type: 'POST',
    data: {
        // Nombre de la propiedad que que recibe el valor de x en el backend
        nombreprodiedad: myfuncion()
    },
    url: 'http://localhost:8080/HelloSpringMVC/hello/' + myfuncion(),
    success: function(response) {
      console.log(response);
      // Hacer algo con la respuesta
    },
    error: function(e) {
      console.log(e);
    }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text" id="login" placeholder="Escribe un numero">
<div id="demo"></div>
<button id="btn">Probar</button>

I hope it serves you.

    
answered by 10.03.2018 / 19:40
source