[OpenLayers-Dev] Sending the "X-Requested-With" header in AJAX calls

Marc Jansen jansen at terrestris.de
Wed Sep 7 02:47:01 EDT 2011


Hi all,

Should we send the X-Requested-With header [1] in AJAX calls as many
other libraries do? Most libraries include this non-standardized header
to show the server that the request is an AJAX-call.

In my particluar case a symfony backend would send different output
depending on whether an URL is retrieved via AJAX or the usual way.

I can provide a patch if that is desired, something roughly looking like
this:

Index: lib/openlayers/trunk/lib/OpenLayers/Request/XMLHttpRequest.js
===================================================================
--- lib/openlayers/trunk/lib/OpenLayers/Request/XMLHttpRequest.js   
(revision 12333)
+++ lib/openlayers/trunk/lib/OpenLayers/Request/XMLHttpRequest.js   
(working copy)
@@ -217,6 +217,9 @@
         }
     };
     function fXMLHttpRequest_send(oRequest) {
+        // always set the X-Requested-With:-header
+        oRequest._object.setRequestHeader("X-Requested-With",
"XMLHttpRequest");
+       
         oRequest._object.send(oRequest._data);
 
         // BUGFIX: Gecko - missing readystatechange calls in
synchronous requests


Best regards,
Marc

[1]
http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Common_non-standard_request_headers


More information about the Dev mailing list