I have some web application that uses jQuery to send AJAX requests to server. One of the methods (in controller) returns a ModelAndView object that can be a html code or JSON object - depends on errors existing. The input parameters of thid method is JSON array (form). The method definition:
@RequestMapping(value = "/generate", method = RequestMethod.POST, headers = BaseController.AJAX_HEADER)
public ModelAndView generate(@RequestBody HandlingReportForm form, HttpServletResponse response) {...}
The jQuery method looks like:
jQuery.ajax({
'type': 'POST',
'url': url,
'contentType': 'application/json',
'data': JSON.stringify(data),
'dataType': 'json',
'success': callback,
error : function(jqXHR, textStatus, errorThrown) {
alert(errorThrown);
},
complete : function(jqXHR, textStatus) {
alert(textStatus);
}
});
There is no problem when server returns a JSON array. The problem comes when server's response is html. I receive a "parseerror" exception. I suppose that jQuery trying to parse the response string to JSON. AM I right? How can I disable that automatic parsing? Is there another way to implement such universal method?
Thank you Thank you