久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從跨域 Ajax 請求訪問 Content-Length 標頭?

How can I access the Content-Length header from a cross domain Ajax request?(如何從跨域 Ajax 請求訪問 Content-Length 標頭?)
本文介紹了如何從跨域 Ajax 請求訪問 Content-Length 標頭?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我的 JavaScript 應用程序需要在使用 Ajax 下載資源之前確定資源的長度.通常這不是問題,您只需發出 HEAD 請求并提取 Content-Length.

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.

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

但是,資源存儲在與客戶端不同的服務器上.(我控制的服務器).所以我使用 CORS 來發出跨域 ajax 請求,并設置服務器來響應 HEAD 請求的預檢請求和帶有自定義標頭的 GET/POST 請求.

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 時,我似乎找不到從 HEAD 響應中提取 Content-Length 的方法:

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:

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

規范似乎建議應該使其可用.但無論我做什么,我似乎都無法使 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?

(鉻 8)

推薦答案

我發現所有瀏覽器對 CORS 響應標頭的支持都存在問題.在 Chrome/Safari 中,我只在 getAllResponseHeaders() 的結果中看到簡單的響應標頭 (http://www.w3.org/TR/cors/#terminology),即使是Access-Control-Expose-Headers"標頭在響應中設置.在 Firefox 3.6.13 中, getAllResponseHeaders() 不返回任何內容(甚至不返回簡單的響應標頭).作為一種解決方法,我想您可以重載一個簡單的響應標頭以包含內容長度,但這可能會導致其他問題,并且仍然無法修復 Firefox.

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.

這篇關于如何從跨域 Ajax 請求訪問 Content-Length 標頭?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 欧洲亚洲一区二区三区 | 黄色综合| 午夜精品一区 | 欧美一区二区免费 | 超碰在线97国产 | 国产激情视频在线观看 | 一级片av | 日韩不卡视频在线观看 | 免费在线观看一区二区 | 欧美日韩在线观看一区二区三区 | 久久99精品久久久久久 | 一色桃子av一区二区 | 日韩国产黄色片 | 91精品国产综合久久婷婷香蕉 | 色婷婷一区二区三区四区 | 毛片一区二区三区 | 亚洲精品视频免费看 | 欧美精品一区二区三区四区 在线 | 国产美女在线观看 | 久久久九九九九 | 久久精品在线免费视频 | 日韩一区二区三区视频在线播放 | 日韩一区二区av | 国产91丝袜在线播放 | 日本淫视频 | 两性午夜视频 | 一区二区免费看 | 密色视频 | 一级黄色淫片 | 九九热在线免费观看 | 丁香久久 | 一区二区三区av | 天天曰天天干 | 国产精品一区二区免费看 | 久久国产精品免费视频 | 国产乱码精品1区2区3区 | 国产成人精品久久二区二区91 | 国产精品久久精品 | 国产精品精品 | 先锋影音资源网站 | 一级黄色毛片免费 |