I'm working on an HTML app in which there is a form. On clicking the submit button, I make a server-side call using jquery.ajax()
. However, whenever an exception is returned from the server, like a Status Code 500
, I need to display an error message on the same page. However, it automatically redirects when it encounters an error. I tried using the statusCode
setting in jquery.ajax() like this:
$.ajax(
{
method: 'GET',
url: 'my url',
//...
successCode: {
500: function(response) {
alert(response.getResponseHeader("xyz"));
$('some_selector').show();
}
},
success: function(){},
error: function(){}
//...
})
But this does not seem to work. It redirects me to the action link and displays the JSON error and I cannot think of a way to prevent this redirect. Any help would be appreciated.
Thanks