0

I have this javascript that should send a JSON to my escreve POST REST method

$(document).ready(function() {
    $("#idform").on('submit', function(e) {
        e.preventDefault();
        alert($("#idform").serialize());
        $.ajax({
            url : 'http://localhost:8080/DBRest/rest/escreve',
            type : "POST", // type of action POST || GET
            dataType : 'json', // data type
            data : $("#idform").serialize() // post data || get data
        })

    });
});

this is my server-side escreve method:

@POST
@Path("escreve")
@Consumes(MediaType.APPLICATION_JSON)
public void escreve(InputStream dado) throws IOException {
    StringBuilder construtor = new StringBuilder();
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(dado));
        String linha = null;
        while ((linha = in.readLine()) != null) {
            construtor.append(linha);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(construtor.toString());
    Pessoa pessoa = gson.fromJson(construtor.toString(), Pessoa.class);
    Repo.escreve(pessoa);       
}

Unfortunately I got this message on F12's Chrome:

jquery.min.js:4 POST http://localhost:8080/DBRest/rest/escreve 415 (Unsupported Media Type)
send @ jquery.min.js:4
ajax @ jquery.min.js:4
(anonymous function) @ index.html:20
dispatch @ jquery.min.js:3
q.handle @ jquery.min.js:3 

With js alert($("#idform").serialize()); I got this: nome=Mary&idade=12 that is clearly not JSON parsed. I now my escreve method works because I tested it with a java class sending a JSON object correctly.

rado
  • 5,720
  • 5
  • 29
  • 51
  • So you have a method that accepts json, you send data that you know is *not* json and you ask why it doesn't work? – f1sh Nov 23 '16 at 15:48
  • http://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery – 1252748 Nov 23 '16 at 15:49
  • 1
    @f1sh I'm asking how this could be done correctly. Obviously I'm not interested only in the final answer. I've already got it. If you have any additional knowledge, please, submit an answer. Otherwise, just downvote and wait someone explain it. – rado Nov 23 '16 at 15:58

3 Answers3

1

I solved changing:

dataType : 'application/json'
rado
  • 5,720
  • 5
  • 29
  • 51
0

Try using serializeArray, which creates an array. Test it for yourself: console.log($("#idform").serializeArray());. serialize creates a query string that's meant to be part of an HTTP request. Both representations are equivalent in the sense that using appropriate code you can convert one to the other without any ambiguity.

Both versions are available because serialize is convenient if you want to make an HTTP request by putting the results in a query string, while serializeArray is more convenient if you want to process the results yourself.

Daerik
  • 4,167
  • 2
  • 20
  • 33
0

Try to send them sepperatly

$.ajax({
            url: 'http://localhost:8080/DBRest/rest/escreve',
            type: 'POST',
            data: { input1: $("#input1").val(), input2: $("#input2").val() },
            success: function(result) {

            }
        });

And if you want to read it in php

echo $_POST["input1"].$_POST["input2"];