I'm developing a system with Spring (Spring Boot) and front-end backend with HTML, CSS (BootStrap) and JavaScript (JQuery).
I'm having trouble building an ajax request.
Follow the Controller Spring code:
@RequestMapping(value = "/visualizarResumo", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody ResumoNFe visualizarResumo(@RequestParam Long empresaId, @RequestParam Long nsu) {
System.out.println(empresaId);
System.out.println(nsu);
return resumoNFeService.buscaPorId(new ResumoPK(empresaId, nsu));
}
And the requisition:
$('button[name=visualizarResumo]').click(function(e) {
var buttonVisualizarResumo = $(this);
var nsu = buttonVisualizarResumo.attr('data-nsu');
var empresaId = buttonVisualizarResumo.attr('data-empresa');
console.log(nsu);
console.log(empresaId);
$.ajax({
url: '/edocs/mde/visualizarResumo',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({empresaId:empresaId, nsu:nsu}),
dataType : 'json',
error: onErrorVisualizarResumo,
success: onSuccessVisualizarResumo
});
function onErrorVisualizarResumo() {
console.log(arguments);
}
function onSuccessVisualizarResumo() {
console.log("sucesso");
}
});
Looking at the Chrome tools, Request Payload is set up right with the company Id and nsu. However a bad request (400) is returned to me
exception : "org.springframework.web.bind.MissingServletRequestParameterException"
message : "Required Long parameter 'companyId' is not present"