Posting a JSon object to Webservice via AJAX

0

I'm trying to send an object in JSon format to my Java Webservice by AJAX, but I was not successful, I already tested wrong formats, which are not JSon's, as "string", and the database reports error properly, and therefore should report that the object was saved, so the html does not return the 'alert' either. Here is my code below:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scriptsrc="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>
    
asked by anonymous 17.01.2018 / 22:54

1 answer

1

You are creating an object in a wrong place, according to the syntax, to correct, change the creation position 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');
  }
});

If you have questions, you can also check here , vlw!

    
17.01.2018 / 23:11