如何从跨域Ajax请求访问内容长度头?

时间:2022-08-23 10:20:11

My JavaScript application needs to determine the length of a resource before downloading it with Ajax. Ordinarily this is not a problem, you just make a HEAD request and extract the Content-Length.

我的JavaScript应用程序需要在使用Ajax下载资源之前确定资源的长度。通常,这不是问题,您只需要做一个HEAD请求并提取内容长度。

var xhr = $.ajax({type:"HEAD", url: "http://own-domain/file.html"})
xhr.getResponseHeader("Content-Length")  
// "2195"

However, the resources are stored on a different server to the client. (A server I control). So I'm using CORS to make cross domain ajax requests, and have set up the server to respond to preflighting requests for HEAD requests and GET/POST requests with custom headers.

但是,资源被存储在不同的服务器上。(我一个服务器控制)。因此,我使用CORS来进行跨域ajax请求,并设置了服务器以响应HEAD请求的预置请求,并使用自定义标头获取/POST请求。

That is working great in the main, but I can't seem to find a way extract the Content-Length from the HEAD response when working with CORS:

这在主要工作中发挥了很大的作用,但我似乎找不到一种方法,在使用CORS时,从头部的响应中提取出内容长度:

var xhr = $.ajax({type:"HEAD", url: "http://other-domain/file.html"})
xhr.getResponseHeader("Content-Length")
// ERROR: Refused to get unsafe header "Content-Length"

I have experimented with setting various headers in the preflighting or in the response, such as

我尝试过在预制或响应中设置不同的标题,例如

Access-Control-Expose-Headers: Content-Length

which the specification seems to suggest should make it available. But no matter what I do, I can't seem to make the Content-Length header available to the client. Any suggestions?

规范似乎建议应该提供它。但是无论我做什么,我似乎都不能让客户端获得内容长度的标题。有什么建议吗?

(Chrome 8)

(铬8)

2 个解决方案

#1


3  

I've found CORS response header support in all browsers to be buggy. In Chrome/Safari, I only see simple response headers (http://www.w3.org/TR/cors/#terminology) in the result of getAllResponseHeaders(), even when the "Access-Control-Expose-Headers" header is set in the response. And in Firefox 3.6.13, getAllResponseHeaders() doesn't return anything (not even simple response headers). As a workaround, I suppose you could overload one of the simple response headers to include the content-length, but that may cause other issues, and still wouldn't fix Firefox.

我在所有浏览器中都发现了CORS响应头支持。在Chrome/Safari中,我只在getAllResponseHeaders()的结果中看到简单的响应头(http://www.w3.org/TR/cors/#术语),即使在响应中设置了“访问-控制-暴露-头”头。在Firefox 3.6.13中,getAllResponseHeaders()不返回任何内容(甚至不返回简单的响应头)。作为一个解决方案,我认为您可以重载其中一个简单的响应头来包含内容长度,但是这可能会导致其他问题,并且仍然不能修复Firefox。

#2


5  

I was having the same problem, till I found a thread somewhere else that taught me to add this line on my .htaccess:

我遇到了同样的问题,直到我在其他地方找到了一个线程,它教会我在。htaccess中添加这一行:

Header add Access-Control-Expose-Headers "Content-Length"

Then BOOM, it got fixed.

然后砰的一声,它被修好了。

#1


3  

I've found CORS response header support in all browsers to be buggy. In Chrome/Safari, I only see simple response headers (http://www.w3.org/TR/cors/#terminology) in the result of getAllResponseHeaders(), even when the "Access-Control-Expose-Headers" header is set in the response. And in Firefox 3.6.13, getAllResponseHeaders() doesn't return anything (not even simple response headers). As a workaround, I suppose you could overload one of the simple response headers to include the content-length, but that may cause other issues, and still wouldn't fix Firefox.

我在所有浏览器中都发现了CORS响应头支持。在Chrome/Safari中,我只在getAllResponseHeaders()的结果中看到简单的响应头(http://www.w3.org/TR/cors/#术语),即使在响应中设置了“访问-控制-暴露-头”头。在Firefox 3.6.13中,getAllResponseHeaders()不返回任何内容(甚至不返回简单的响应头)。作为一个解决方案,我认为您可以重载其中一个简单的响应头来包含内容长度,但是这可能会导致其他问题,并且仍然不能修复Firefox。

#2


5  

I was having the same problem, till I found a thread somewhere else that taught me to add this line on my .htaccess:

我遇到了同样的问题,直到我在其他地方找到了一个线程,它教会我在。htaccess中添加这一行:

Header add Access-Control-Expose-Headers "Content-Length"

Then BOOM, it got fixed.

然后砰的一声,它被修好了。