error 400 (bad request) with jquery

1

friends using the method .POST of jquery send some parameters to a servlet mapped with spring , after that I need to return a value to the view, for this I transform the String to JSON with the library GSON , but when I return that value I skip that error 400 (bad request), which could be doing wrong? Here I leave the code

Javascript file:

function sesion(){
        console.log('inicio de envio de parametros a servlet');
        formulario = $('#formBus');
        $.post('ConsultasRms/autenticar.html',formulario.serialize(),function (data,status){
            console.log('entro a función de sesión data: '+data+' estatus: '+status);
            $("#res").html(data);
        }),"JSON";

servlet:

       @RequestMapping("/autenticar")
    public String login(HttpServletRequest request, HttpServletResponse response,
                                       @RequestParam("j_username") String user,
                                       @RequestParam("j_password") String pass
    ) throws Exception {

        logger.info("Inicio de metodo login con paramatros usuario: "+user+" y pass: "+pass);
        System.out.println("Inicio de metodo login con paramatros usuario: "+user+" y pass: "+pass);

        String direccion = "index.jsp";

        Gson gson = new Gson();
        String retorno = gson.toJson(direccion);

       // FacesContext context = FacesContext.getCurrentInstance();
       // request = (HttpServletRequest) context.getExternalContext().getRequest();

        if(user.contains("prueba") && pass.contains("1234")){
            System.out.println("autenticado, retorno json: "+retorno);

            return retorno;
        }
        return null;
    }
}
    
asked by Jdeveloper 09.08.2017 в 15:20
source

1 answer

1

Well actually I'm not an expert and I'm also starting to see Spring but I think you should assign it to the method you are trying to use the method by which the request will be made ie: In @RequestMapping(value = "/autenticar") you should also indicate the method by which the request will be executed, I think that's why it says Bad Request or error in the request would be lake like:

@RequestMapping(value = "/autenticar", method = RequestMethod.POST)

I hope I'll help you with something.

    
answered by 09.08.2017 в 15:38