Search code examples
javaajaxspringdwr

DWR remote calls not setting X-Requested-With request header with value XMLHttpRequest


We are using spring dwr implementation to execute ajax calls. Snippet from dwr_provider_xml looks like...

<bean id="remoteClass"
    class="x.y.z.RemoteImpl">
    <dwr:remote javascript="AjaxSessionManager">
        <dwr:include method="testMethod" />
    </dwr:remote>
</bean>

Javascript call:

if ( AjaxSessionManager != undefined  ) {
    AjaxSessionManager.testMethod();
}

We have, in server side, some common code to check whether this call is an ajax call and do some additional steps.

private static final String XMLHTTPREQ = "XMLHttpRequest";
public static boolean isAjaxCall(HttpServletRequest request) {
    return XMLHTTPREQ
         .equals(request.getHeader("X-Requested-With"));
}

But this method is never returning true and when I traced through Fiddler, the request header is not being sent. I believe jQuery, dojo and other popular frameworks by default set this. Do we have to define something for DWR, explicitly? Any help is appreciated.


Solution

  • I found a solution. DWR provides a headers option to be passed, so my method call now looks like this...

    AjaxSessionManager.testMethod({callback:{},
    headers: {'X-Requested-With':'XMLHttpRequest'}
    );