Jquery jqXHR.getResponseHeader ('Location'), null

in my ajax call is responseHeader('Location')always FF. Can anybody help me? By the way, this is 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
                });
            });
+4
source share
2 answers

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

Access-Control-Expose-Headers: Location

This will allow the firefox browser to allow 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

+7
source

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

, Firefox/jQuery, , . :

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;
    }
  });

});
+2

Source: https://habr.com/ru/post/1530653/


All Articles