在JavaScript中访问网页的HTTP标头 [英] Accessing the web page's HTTP Headers in JavaScript

查看:255
本文介绍了在JavaScript中访问网页的HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过JavaScript访问网页的HTTP响应标头?

How do I access a page's HTTP response headers via JavaScript?

此问题 ,已被修改为询问有关访问两个特定HTTP标头的信息。

Related to this question, which was modified to ask about accessing two specific HTTP headers.


相关:

如何通过JavaScript访问HTTP请求标题字段?


推荐答案

不幸的是,没有一个API可以为您的初始页面请求提供HTTP响应标头。那是这里发布的原始问题。它反复也问,因为有些人想要获得原始页面请求的实际响应标题而不发出另一个。

Unfortunately, there isn't an API to give you the HTTP response headers for your initial page request. That was the original question posted here. It has been repeatedly asked, too, because some people would like to get the actual response headers of the original page request without issuing another one.

如果通过AJAX发出HTTP请求,则可以使用 getAllResponseHeaders()获取响应头 方法。它是XMLHttpRequest API的一部分。要了解如何应用此功能,请查看下面的 fetchSimilarHeaders() 函数。请注意,对于某些应用程序来说,这是一个解决问题的方法。

If an HTTP request is made over AJAX, it is possible to get the response headers with the getAllResponseHeaders() method. It's part of the XMLHttpRequest API. To see how this can be applied, check out the fetchSimilarHeaders() function below. Note that this is a work-around to the problem that won't be reliable for some applications.

myXMLHttpRequest.getAllResponseHeaders();




  • API在XMLHttpRequest的以下候选推荐中指定: XMLHttpRequest - W3C候选人推荐2010年8月3日

    具体来说, getAllResponseHeaders()方法在以下部分中指定: w3.org: XMLHttpRequest getallresponseheaders()方法

    Specifically, the getAllResponseHeaders() method was specified in the following section: w3.org: XMLHttpRequest: the getallresponseheaders() method

    MDN文档也很好: developer.mozilla.org: XMLHttpRequest

    The MDN documentation is good, too: developer.mozilla.org: XMLHttpRequest.

    这不会为您提供有关原始页面请求的HTTP响应标头的信息,但它可用于进行有根据的猜测关于那些头是的。接下来将对此进行详细介绍。

    This will not give you information about the original page request's HTTP response headers, but it could be used to make educated guesses about what those headers were. More on that is described next.

    这个问题几年前首次被问到,具体询问如何获取当前页面的原始HTTP响应头(即javascript运行的同一页面)。这是一个完全不同的问题,而不仅仅是获取任何HTTP请求的响应头。对于初始页面请求,标头不可用于javascript。如果您通过AJAX再次请求同一页面,您所需的标题值是否可靠且足够一致将取决于您的特定应用程序。

    This question was first asked several years ago, asking specifically about how to get at the original HTTP response headers for the current page (i.e. the same page inside of which the javascript was running). This is quite a different question than simply getting the response headers for any HTTP request. For the initial page request, the headers aren't readily available to javascript. Whether the header values you need will be reliably and sufficiently consistent if you request the same page again via AJAX will depend on your particular application.

    以下是一些建议解决这个问题。

    The following are a few suggestions for getting around that problem.

    如果响应基本上是静态的,并且请求之间的标题不会发生很大变化,那么您可以为同一页面发出AJAX请求您当前正在使用并假设它们是相同的值,这些值是页面HTTP响应的一部分。这可以让你使用上面描述的漂亮的XMLHttpRequest API访问你需要的头文件。

    If the response is largely static and the headers are not expected to change much between requests, you could make an AJAX request for the same page you're currently on and assume that they're they are the same values which were part of the page's HTTP response. This could allow you to access the headers you need using the nice XMLHttpRequest API described above.

    function fetchSimilarHeaders (callback) {
        var request = new XMLHttpRequest();
        request.onreadystatechange = function () {
            if (request.readyState === 4) {
                //
                // The following headers may often be similar
                // to those of the original page request...
                //
                if (callback && typeof callback === 'function') {
                    callback(request.getAllResponseHeaders());
                }
            }
        };
    
        //
        // Re-request the same page (document.location)
        // We hope to get the same or similar response headers to those which 
        // came with the current page, but we have no guarantee.
        // Since we are only after the headers, a HEAD request may be sufficient.
        //
        request.open('HEAD', document.location, true);
        request.send(null);
    }
    

    如果你真的必须依赖价值保持一致,这种方法会有问题在请求之间,因为你不能完全保证它们是相同的。这将取决于您的具体应用程序,以及您是否知道您需要的价值不会从一个请求更改为下一个请求。

    This approach will be problematic if you truly have to rely on the values being consistent between requests, since you can't fully guarantee that they are the same. It's going to depend on your specific application and whether you know that the value you need is something that won't be changing from one request to the next.

    浏览器通过查看标题来确定一些BOM属性(浏览器对象模型)。其中一些属性直接反映HTTP标头(例如 navigator.userAgent 设置为HTTP User-Agent 的值标题字段)。通过嗅探可用的属性,您可以找到所需的内容,或者一些线索来指示HTTP响应包含的内容。

    There are some BOM properties (Browser Object Model) which the browser determines by looking at the headers. Some of these properties reflect HTTP headers directly (e.g. navigator.userAgent is set to the value of the HTTP User-Agent header field). By sniffing around the available properties you might be able to find what you need, or some clues to indicate what the HTTP response contained.

    如果您控制服务器端,则可以在构建完整响应时访问任何您喜欢的标头。值可以通过页面传递给客户端,存储在某个标记中,也可以存储在内联的JSON结构中。如果您希望将每个HTTP请求标头都用于您的javascript,您可以在服务器上迭代它们并将它们作为隐藏值发送回标记中。以这种方式发送标头值可能并不理想,但您当然可以针对您需要的特定值执行此操作。这个解决方案也可能效率低下,但是如果你需要的话它会起作用。

    If you control the server side, you can access any header you like as you construct the full response. Values could be passed to the client with the page, stashed in some markup or perhaps in an inlined JSON structure. If you wanted to have every HTTP request header available to your javascript, you could iterate through them on the server and send them back as hidden values in the markup. It's probably not ideal to send header values this way, but you could certainly do it for the specific value you need. This solution is arguably inefficient, too, but it would do the job if you needed it.

    这篇关于在JavaScript中访问网页的HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆