0

I need to get the data from a table, allocate it in an array and send it via Json to my Java controller and finally return a Model and View. However, this request can not be ajax due to the return of my controller being a page.

I did it this way as the $ .post documentation:

var orders = new Array();
$("#table-order tbody tr").each(function (){
        var col = $(this).children();

        if($(this).find('input').is(':checked')) {
         var order = {
           'number' : $(col[0]).text(),
           'desc' : $(col[1]).text(),
           'obs' : $(col[2]).text()     
          };
         orders.push(order);
        }
});

$.post( "sendToPrint", { array: JSON.stringify(orders) }, function( response ) {
          console.log( response );            
}, "json");

My Controller:

@RequestMapping(value="/sendToPrint", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
@Transactional("productTransactionManager")
public ModelAndView imprimir(@RequestBody String ordens) {
    ModelAndView mv = new ModelAndView("impressao/impressao");
    Gson gson = new Gson();
    List<OrdemServico> listaNova =  new ArrayList<>();
    ArrayList<OrdemServico> listaOrdens = (ArrayList<OrdemServico>) gson.fromJson(ordens, new TypeToken<ArrayList<OrdemServico>>(){}.getType());
    for (OrdemServico ordemServico : listaOrdens) {
         int mes = LocalDateTime.now().getMonthValue();
         int ano = LocalDateTime.now().getYear();
         int dia = LocalDateTime.now().getDayOfMonth();
         LocalDateTime dataHora = LocalDateTime.of(ano, mes, dia, 0, 0, 0);
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
         String data = dataHora.format(formatter);
         ordemServico.setData(data);
        listaNova.add(ordemServico);
    }
    mv.addObject("lista", listaNova);
    return mv;
}

The return error:

415 (Unsupported Media Type)

scooby
  • 207
  • 1
  • 12
  • 2
    Possible duplicate of [Jquery - How to make $.post() use contentType=application/json?](http://stackoverflow.com/questions/2845459/jquery-how-to-make-post-use-contenttype-application-json) – aUserHimself Apr 19 '17 at 12:48
  • *"How to send a post request without AJAX?"* ~ use input boxes and submit (1990 style) – Tilak Madichetti Apr 19 '17 at 12:50
  • Is not a duplicate post, because in my case I can not use $ ajax – scooby Apr 19 '17 at 12:50
  • I need a non-ajax request because of my return Model and View, and neither am I not sending a form but data from a table. – scooby Apr 19 '17 at 12:52
  • I have a big problem my friends, I can not just submit a table because I need to send only the data of the rows selected by the user and without ajax lol – scooby Apr 19 '17 at 12:57

2 Answers2

1

You can post your data with javascript like that;

var xhr = new XMLHttpRequest();
xhr.open("POST", url , TRUE);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({value:value}));
eisbach
  • 417
  • 3
  • 7
0

Problem occurred because you have specified JSON as the data-type that this controller will consume and you're passing incomplete information in your ajax call. You need to add necessary headers to your ajax call. They are :

headers: { 
    'Accept': 'application/json',
    'Content-Type': 'application/json' 
},
Durlabh Sharma
  • 397
  • 2
  • 14