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

Xavier Mamano (jorix) xavier.mamano at gmail.com
Wed Sep 7 16:20:33 EDT 2011


Hi,

Great!

But "Request.XMLHttpRequest" is not written by OpenLayers Team (see license
in XMLHttpRequest.js) and is fully wrapped by "Request", so I think best
make the following change:

Index: lib/OpenLayers/Request.js
===================================================================
--- lib/OpenLayers/Request.js	(revision 12340)
+++ lib/OpenLayers/Request.js	(working copy)
@@ -122,6 +122,10 @@
             {proxy: OpenLayers.ProxyHost}
         );
         config = OpenLayers.Util.applyDefaults(config, defaultConfig);
+        config.headers = OpenLayers.Util.applyDefaults(
+            config.headers,
+            {"X-Requested-With": "XMLHttpRequest"}
+        );
 
         // create request, open, and set headers
         var request = new OpenLayers.Request.XMLHttpRequest();

This has the same effect as your proposal.


Xavier Mamano
---


Marc Jansen-2 wrote:
> 
> 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
> _______________________________________________
> Dev mailing list
> Dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-dev
> 


--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Sending-the-X-Requested-With-header-in-AJAX-calls-tp6766726p6769388.html
Sent from the OpenLayers Dev mailing list archive at Nabble.com.


More information about the Dev mailing list