In Spring MVC, if I submit web form using normal submit I can handle 404 exception in web.xml
as
<error-page>
<error-code>404</error-code>
<location>404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>404.jsp</location>
</error-page>
But how to intercept 404
error from ajax call (probably using @ControllerAdvice
) and pass custom exception to xhr.responseText
in jquery
?
You could use a default controller for unmapped requests, and write your error in the response:
Then you can get the error in your javascript:
Obviously this only works for request handled by your DispatcherServlet,