CORS and firefox and datajs problem

Jul 15, 2011 at 3:46 PM

Hi guys,


i'm now trying to read an odata service with using CORS. To support this the server has to respond with specific headers like:

'Access-Control-Allow-Origin: *'

 'Access-Control-Allow-Methods: GET

'Access-Control-Expose-Headers: Content-Type, Content-Length 

Now datajs calls the readResponseHeaders(xhr, headers); method and in there xhr.getAllResponseHeaders().

Now this returns null when using firefox 3.5 and Firefox 5 returns an empty string although I exposed some headers as seen above.

So I could not get the response type (if it was xml or JSON)  and so on the datajs handler can't handle and process the response correctly...

Calling xhr.getResponseHeader(„Content-Type“);.  for example worked.


So in the end I would say that this is a firefox bug where firefox doesn't return the requested CORS headers.


You can try it yourself by using this samply by John Resig...


Did you guys also tried datajs with CORS and ran into similar problems? If yes is there a way that datajs can handle this?

Otherwise we have to wait for fixes in firefox. Also I don't know about if and how other browsers already support CORS.


PS: Also interesting to read:



Jul 19, 2011 at 5:25 PM

Hi Joe82,

We didn't specifically try CORS since it's not a popular standard right now. I think you pinpointed the issue in Firefox in the stackoverflow link you mentioned. Unfortunately, if there's a bug in the browser in the handling of those headers there's no way for datajs to get around that at this point. Hopefully the bug has been reported and will be fixed by Firefox.


Jul 20, 2011 at 11:53 AM

yeah...for now I adapted datajs to check if getAllresponseHeaders returns anything and if not then I try implicitly to get the needed headers with getResponseHeader, so it could work with the firefox bug:


var readResponseHeaders = function (xhr, headers) {
/// <summary>Reads response headers into array.</summary>
/// <param name="xhr" type="XMLHttpRequest">HTTP request with response available.</param>
/// <param name="headers" type="Array">Target array to fill with name/value pairs.</param>

var responseHeaders = xhr.getAllResponseHeaders();
   if (!responseHeaders){
     var contentType = xhr.getResponseHeader("Content-Type");
     var contentLength = xhr.getResponseHeader("Content-Length");
     if (contentType)
       headers["Content-Type"] = contentType;
     if (contentLength)
       headers["Content-Length"] = contentLength;
   responseHeaders = responseHeaders.split(/\r?\n/);
   var i, len;
   for (i = 0, len = responseHeaders.length; i < len; i++) {
     if (responseHeaders[i]) {
       var header = responseHeaders[i].split(": ");
       headers[header[0]] = header[1];