Skip to content

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

Open

Description

@AeroBuffalo

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);
}
};
httpSessionID.open("GET", "http://www.google.com/", false);
httpSessionID.send();
var something = JSON.parse(httpSessionID.getAllResponseHeaders());
(or)
var something = httpSessionID.getResponseHeader('set-cookie');
--------------------------------------[End Code]--------------------------------------

The complete error I get for when I call the second one (getResponseHeader) is:
--------------------------------------[Begin Error]--------------------------------------
.../node_modules/xmlhttprequest/lib/XMLHttpRequest.js:210
&& 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/webrtc.io/lib/webrtc.io.js:358:35
at Object.rtc.fire (.../node_modules/webrtc.io/lib/webrtc.io.js:62:15)
at WebSocket. (.../node_modules/webrtc.io/lib/webrtc.io.js:91:11)
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]--------------------------------------

Activity

  1. AeroBuffalo commented on Nov 13, 2012

    @AeroBuffalo
    Author

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

  2. driverdan commented on Nov 13, 2012

    @driverdan
    Owner

    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.

  3. andrewspode commented on Dec 6, 2013

    @andrewspode
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions