Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xhr.response is undefined on blob/arraybuffer response type #166

Open
nolash opened this issue Jun 20, 2018 · 6 comments
Open

xhr.response is undefined on blob/arraybuffer response type #166

nolash opened this issue Jun 20, 2018 · 6 comments

Comments

@nolash
Copy link

nolash commented Jun 20, 2018

Trying to get this file with your nodejs xmlhttprequest

$ curl -X GET -I http://localhost/test/foo.bin
HTTP/1.1 200 OK
Date: Wed, 20 Jun 2018 11:58:51 GMT
Server: Apache/2.4.33 (Unix)
Last-Modified: Tue, 19 Jun 2018 18:37:07 GMT
ETag: "6-56f02f8230618"
Accept-Ranges: bytes
Content-Length: 6
Content-Type: application/octet-stream

using the following code:

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

        var xhr = new XMLHttpRequest;
        xhr.open("GET", "http://localhost/test/foo.bin");
        xhr.setRequestHeader("Accept", "application/octet-stream");
        xhr.responseType = "blob";
        xhr.onreadystatechange = function(e) {
                if (xhr.readyState == 4) {
                        console.log(xhr.response);
                        console.log(xhr.responseText);
                }
        };      
        xhr.send();

Result is undefined for the logline with xhr.response and correct value in xhr.responseText.

Does your module only work for text?

@marlinilram
Copy link

marlinilram commented Jul 4, 2018

Hi @nolash I met the same problem here, do you have any work around now?

@mannebusk
Copy link

Had the same issue with responseType "text".
For me this worked as a workaround:

Object.defineProperty(
    XMLHttpRequest,
    'response',
    { get: () => this.responseText }
);

Note that you could change get function to check this.responseType and choose the appropriate property instead of just running this.responseText blindly ;)

@bksubhuti
Copy link

bksubhuti commented Mar 12, 2020

Is this fixed yet.. I want to get this to work.. I also get "undefined". The whole purpose of this lib is to run code already written and not change it.
but how to get this to work? Do I need to edit the library by hand?
the object in the debugger says undefined under responseText. Not sure what needs to be in the get function if it is not there.

@KR1470R
Copy link

KR1470R commented Jul 3, 2020

Just use http.get.

@vdenisenko-waverley
Copy link

Any updates in that?

@bksubhuti
Copy link

i'm just using the node library for file reads now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants