Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip the servlet filter for ajax request

How can I skip the execution in doFilter for only an Ajax request?

As of now I am passing parameter called isAjax. Is there a "standard" way to do this?

How can I stop execution in filter in this scenario? Will adding return statement be a proper approach?

like image 816
Suresh Atta Avatar asked Sep 13 '25 04:09

Suresh Atta


1 Answers

Most browsers add a special request-header when sending AJAX requests. You can easily check that using this snippet:

public static boolean isAjaxRequest(HttpServletRequest request) {
    return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
}

Note, however, that this is no perfect guarantee, as this header starts with X-, it is not a standard header and browsers / client code may decide not to send it.

like image 81
mthmulders Avatar answered Sep 16 '25 07:09

mthmulders