Cannot read property 'headers' of undefined in XMLHttpRequest.js #47

AeroBuffalo opened this Issue Nov 13, 2012 · 3 comments


None yet

3 participants


I am trying to get the response headers from an httprequest, and no matter which function I use (getResponseHeader or getAllResponseHeaders), I get the error:
"TypeError: Cannot read property 'headers' of undefined' coming from /xmlhttprequest/lib/XMLHttpRequest.js

The code that I am executing is provided below.
--------------------------------------[Begin Code]--------------------------------------
var httpSessionID = new XMLHttpRequest();
httpSessionID.onreadystatechange = function() {
if (this.readyState == 4) {
console.log("Body:\n" + this.responseText);
};"GET", "", false);
var something = JSON.parse(httpSessionID.getAllResponseHeaders());
var something = httpSessionID.getResponseHeader('set-cookie');
--------------------------------------[End Code]--------------------------------------

The complete error I get for when I call the second one (getResponseHeader) is:
--------------------------------------[Begin Error]--------------------------------------
&& response.headers[header.toLowerCase()]
TypeError: Cannot read property 'headers' of undefined
at exports.XMLHttpRequest.getResponseHeader (.../node_modules/xmlhttprequest/lib/XMLHttpRequest.js:210:18)
at .../node_modules/
at (.../node_modules/
at WebSocket. (.../node_modules/
at WebSocket.EventEmitter.emit (events.js:96:17)
at Receiver.self._receiver.ontext (.../node_modules/ws/lib/WebSocket.js:539:10)
at Receiver.opcodes.1.finish (.../node_modules/ws/lib/Receiver.js:397:14)
at Receiver.opcodes.1.getData as expectHandler
at Receiver.add (.../node_modules/ws/lib/Receiver.js:93:24)
at Socket.firstHandler (.../node_modules/ws/lib/WebSocket.js:519:22)
--------------------------------------[End Error]--------------------------------------


Never mind. I just read that it is because of the synchronous flag. Asynchronous still works.


You were using async calls improperly but it looks like you did find a bug. It shouldn't throw this error when calling the header methods at invalid times.

To make this async call properly you need to call the header methods after readyState == 4. I strongly recommend avoiding synchronous calls since the code to handle them needs some refining and it goes against the async methodology of node.


I'm coming across this bug VERY rarely, and I think it's because I'm getting no proper response from the server. I'm doing a pull request with my patch for this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment