0

I'm sending a POST request to my Spring MVC controller. I've been tweaking the code for hours and getting diverse errors from the controller, such as 400, 404 or 415.

Finally, now my POST request is working (as the controller logs all the data to the console), but the Ajax successcallback is never triggered but the fail check function always is.

My Ajax request:

        $.ajax({
                'type': 'POST',
                'url': '/iQuality/post-test',
                'data': $("#wizard-form").serializeJSON(), 
                'dataType': 'json',
                'headers': {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json' 
                },
                'success': function(){
                    console.log('mandarina');
                }
            })
            .fail(alert("Error!"));

My Spring controller:

@RequestMapping(value = "/post-test", method = RequestMethod.POST, headers = { "Content-type=application/json" })
@ResponseStatus(value = HttpStatus.OK)
private @ResponseBody void handleWizardPost(@RequestBody WizardForm1 wf){

    logger.debug("[post-test] : Called route");

    logger.info(wf.getSistema());
    logger.info(wf.getNombrePase());
    logger.info(wf.getEsAtipico());

}

BTW, the controller is supposed to send an empty response (I want the view to stay in the same page, no page refresh).

houcros
  • 1,000
  • 1
  • 14
  • 32

1 Answers1

0

The post parameters include this:

'dataType': 'json',

Your controller is probably not responding with a properly formatted json string, and then jQuery in turn fails to parse the response as a json, and your success callback is never called. If you have an empty response, try to send '{}' and it will fix the problem.

Alternatively you can just remove the 'dataType': 'json' from your parameters, it will default to 'string, and jQuery won't try to parse it, and will call your success callback.

Ronen Cypis
  • 21,182
  • 1
  • 20
  • 25
  • This is strange... I did as you suggest and now both, the success and fail functions, get executed! I tried the same plus removing the `@ResponseBody` from the method signature of the controller and I get the same result. – houcros Dec 04 '15 at 11:02
  • check this: http://stackoverflow.com/questions/10931836/should-i-use-done-and-fail-for-new-jquery-ajax-code-instead-of-success-and – Ronen Cypis Dec 06 '15 at 13:43