XMLHttpRequest returns only empty responseText in one project in other one is working

Hi,

I try to execute the following code as scratch:

 

const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

var data = null;

var xhr = new XMLHttpRequest();

xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(xhr.responseText);

console.log('\nlog: ' + JSON.stringify(xhr));
}
});

xhr.open("GET", "http://www.google.de/");

xhr.send(data);


I've installed xmlhttprequest via npm install in same version in both projects (1.8.0). In one project I get a response.
In the other one I just get:
log: {"UNSENT":0,"OPENED":1,"HEADERS_RECEIVED":2,"LOADING":3,"DONE":4,"readyState":4,"onreadystatechange":null,"responseText":"","responseXML":"","status":0,"statusText":null,"withCredentials":false}

the idea.log file shows nothing while executing.

So I'm wondering what is the problem, in which ways this projects are different and why is this important in a scratch? I even put the working-dir
to ~/tmp that no resource files do something behind the scenes...

But still is not working...

Thank you for your help and kind regards
Christian
0
7 comments

This code works fine for me - response is printed to console.

How do you execute your code? And why do you think that the issue has any relation to WebStorm?

0
Avatar
Permanently deleted user

In one project/workspace it works in the other it doesn't. The webstorm link entered by accident. I corrected it to Idea Users. I think this is better fitting...

But still my problem remains.

0

>I corrected it to Idea Users. I think this is better fitting...

 

No - your issue unlikely has any relation to the IDE. It's not Idea that runs your code, and it's not Idea that responds to your request:)

Usually

"status":0

means the request failed at the very beginning (because the network can't be reached, for example).

Try changing your code to

xhr.addEventListener("readystatechange", function() {
if (this.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText);

console.log('\nlog: ' + JSON.stringify(xhr));
}
else {
console.error(xhr.statusText);
}
}
});

returned error text may give you a clue

0
Avatar
Permanently deleted user

Thank you for the answers so far, the changed code produces just a "null" so the xhr.statusText is not helping much and the documentation on https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/statusText is not really a help. Null is not mentioned there.

I think it has to do something with the IDE because in one it is working in the other it doesn't work and as far as I can compare both run-configurations are identical.

0
Avatar
Permanently deleted user

I checked out the project a second time and just run the scratch.js and there it worked... I've no idea why it is not working in one project but working in a copy of the project... Is this not looking like an IDE problem ;) ?

0
Avatar
Permanently deleted user

So it seems to be a problem of my mocha configuration. I thought the scratches are executed independently but that's not the case. After I configured the whole project the null happened again... So I have to find the flaw in my configuration. But thank you very much for your help

Kind regards

   Christian

0
Avatar
Permanently deleted user

Just to clarify this case. In deed it has nothing to do with the project/workspace or even the IDE. There was something happening in the process of testing that lead to this behavior. In deed the xmlhtmlrequest-library was "patched" to this bad behavior. So no problem of idea but thanks a lot for the provided help.

Kind regards

   Christian

0

Please sign in to leave a comment.