Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery jqXHR.getResponseHeader('Location'), is null

Tags:

jquery

ajax

cors

in my ajax call the responseHeader('Location') is FF always empty. Can anybody help me? By the way it is a CORS.

$.ajax({
                url: VIDEOS_UPLOAD_SERVICE_URL,
                method: 'POST',
                contentType: 'application/json',
                headers: {
                    Authorization: 'Bearer ' + accessToken,
                    'x-upload-content-length': file.size,
                    'x-upload-content-type': file.type
                },
                data: JSON.stringify(metadata)
            }).done(function(data, textStatus, jqXHR) {
                resumableUpload({
                    url: jqXHR.getResponseHeader('Location'),
                    file: file,
                    start: 0
                });
            });
like image 909
user2831042 Avatar asked Mar 07 '14 17:03

user2831042


2 Answers

You can fix this on the server site of the code by setting the headers:

Access-Control-Expose-Headers: Location

That will tell the firefox browser to allow the cross-domain reading of the Location: header.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control#Access-Control-Expose-Headers

like image 190
Raul Nohea Goodness Avatar answered Nov 15 '22 08:11

Raul Nohea Goodness


Looks like it's a bug in Firefox/jQuery: http://bugs.jquery.com/ticket/11624

Looks like it may only affect certain versions of Firefox/jQuery, and there is a patch that may resolve it. From the bug tracker another solution was posted too:

define("jquery-cors-patch", ["jquery"], function ($) {

  // workaround for Firefox CORS bug - see http://bugs.jquery.com/ticket/10338

  var _super = $.ajaxSettings.xhr;
  $.ajaxSetup({
    xhr: function() {
      var xhr = _super();
      var getAllResponseHeaders = xhr.getAllResponseHeaders;
      xhr.getAllResponseHeaders = function() {
          var allHeaders = getAllResponseHeaders.call(xhr);
        if (allHeaders) {
            return allHeaders;
        }
        allHeaders = "";
        var concatHeader = function(i, header_name) {
          if (xhr.getResponseHeader(header_name)) {
            allHeaders += header_name + ": " + xhr.getResponseHeader( header_name ) + "\n";
          }
        };
        // simple headers (fixed set)
        $(["Cache-Control", "Content-Language", "Content-Type", "Expires", "Last-Modified", "Pragma"]).each(concatHeader);
        // non-simple headers (add more as required)
        $(["Location"] ).each(concatHeader);        
        return allHeaders;
      };
      return xhr;
    }
  });

});
like image 21
Igor Avatar answered Nov 15 '22 06:11

Igor