You are here: Reference > JavaScript > client-side > xml handling > methods > getResponseHeader (XMLHttpRequest)

getResponseHeader method (XMLHttpRequest)

Browser support:
Returns the value of the specified response header.
The response headers contain information about the server and the retrieved content ('Content-Length', 'Content-Type', ...). To get all response headers together, use the getAllResponseHeaders method.

Syntax:

object.getResponseHeader (name);
You can find the related objects in the Supported by objects section below.

Parameters:

name
Required. String that specifies the name of the response header to retrieve. The following list contains some of the commonly used names. If you need a more complete list, please visit the HTTP response headers (Wikipedia) page.
Content-Length
The size of the response body, in bytes.
Content-Type
The content type of the response body.
Date
The current server time when the response was sent.
Last-Modified
The date and time when the resource was last modified.
Server
The type and version of the web server.

Return value:

String. The value of the specified response header.

Example HTML code 1:

This example illustrates the use of the getResponseHeader method:
Code
ajax.js
news.xml
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
        var httpRequest = null;

        function SendRequest () {
            if (!httpRequest) {
                httpRequest = CreateHTTPRequestObject ();   // defined in ajax.js
            }
            if (httpRequest) {          
                    // The requested file must be in the same domain that the page is served from.
                var url = "news.xml";
                httpRequest.open ("GET", url, true);    // async
                httpRequest.onreadystatechange = OnStateChange;
                httpRequest.send (null);
            }
        }

        function OnStateChange () {
            if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
                if (IsRequestSuccessful (httpRequest)) {    // defined in ajax.js
                    Test_ResponseHeaders ();
                }
                else {
                    alert ("Operation failed.");
                }
            }
        }

        function Test_ResponseHeaders () {
            var contType = httpRequest.getResponseHeader ("Content-Type");
            var contLength = httpRequest.getResponseHeader ("Content-Length");
            var lastMod = httpRequest.getResponseHeader ("Last-Modified");
            alert ("Content-Type: " + contType + "\n" + 
                   "Content-Length: " + contLength + "\n" +
                   "Last-Modified: " + lastMod);
        }

    </script>
</head>
<body>
    <button onclick="SendRequest ()">Test response headers</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content