Post a Json object to Webservice via AJAX

Asked

Viewed 1,740 times

0

I am trying to send an object in Json format to my Java Webservice by AJAX, but I have not succeeded, I have tested wrong formats, which are not Json’s, as "string", and the database reports error properly, and therefore should report that the object has been saved, similarly html also does not return 'Alert'. Follow my code below:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="scripts/jquery.min.js.download"></script>
 <script src="scripts/jquery.imagemapster.js.download"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
       $.ajax({
                    var myJSon = {"Aluno":"nome", "Respostas":[], "Sessao":[] };
                    type: "POST",
                    url: 'http://localhost:8080/Servidor/server',
                    dataType: "json",
                    data: JSON.stringify(myJSon),
                    sucess: function (data){
                        alert('Sucess');
                    },
                    error: function () {
                        alert('Error');
                    }
        });
    });
});
</script>
</head>
<body>

<button>Send an HTTP POST request to a page and get the result back</button>

</body>
</html>

1 answer

1

You are creating an object within an improper location, according to the syntax, to fix, change the creation position to before the Ajax call and everything will work!

var myJSon = {"Aluno":"nome", "Respostas":[], "Sessao":[] };

$.ajax({
  type: "POST",
  url: 'http://localhost:8080/Servidor/server',
  dataType: "json",
  data: JSON.stringify(myJSon),
  sucess: function (data){
      alert('Sucess');
  },
  error: function () {
      alert('Error');
  }
});

In case of doubts you can also consult here, vlw!

  • thank you very much, now html error alert, but no appearing on the server console, I will keep looking, but again thank you

  • I understand, so check if it is configured in the correct way your back-end sripts, a good site for studies is the official MDN and PHP. This line of PHP works with your code above: <? php print_r(json_encode($_post['Student']); ?>

  • The back-end is in Java

  • ai i don’t manjo, but edits the category up there and puts java too, tries to describe to someone to help with java on the current issue, like, how do I receive the data via Ajax with Java in the back-end.

Browser other questions tagged

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