1 year ago
#384388
Axsis
ReferenceError: XMLHttpRequest is not defined
Before I say anything, yes, I have tried many solutions on the site. I will go in detail on what I have tried later in the post. I am running the below code to import an xml file in the same directory as this js file. Inside the if statement, when the file is loaded, it sends its contents to an html file also in the same directory, as well as running fillCourseSelection(), which does some unrelated stuff with the xml file data.
loadXMLFile(){
//let XMLHttpRequest = require("xhr2");
let httpReq = new XMLHttpRequest();
httpReq.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
document.getElementById("test").innerHTML = this.responseText;
this.fillCourseSelection(this.XMLFileName);
}
};
httpReq.open("GET", this.XMLFileName, true);
httpReq.send();
Running this code inside the js file on WebStorm gives me the error in the title, saying that the XMLHttpRequest is not defined.
Before I list things I have tried, this code does get inside the if statement, but only if its imported and run on the html file this code is being sent to. aka, it works on our website, but the reason I need to fix the "error" is because my debugger can't get past that "let httpReq = new XMLHttpRequest();" line. Therefore, I can't figure out what is wrong with the rest of my code in this.fillCourseSelection() until I fix this error.
Things I have tried:
- I have tried using the require("xhr2"); line you can see I commented out for the import so it recognizes what the request is. All it does is give me this error when I call the send() method: throw new NetworkError(
Unsupported protocol ${this._url.protocol}
); the debug shows readyState = 1 and status = 0. - I have tried using let XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; instead of the xhr2 line. This fixes the error in the js file, but now when I run the html file, the if statement fails and does not send the data to the html file. When I debug run the code, this.readyState = 1 and this.status = 0.
I have done these two steps with the correct imports by running npm commands found on the Stack Overflow forums where these solutions came from. I'm sorry if there is an answer out there already, but I have been looking for hours and I can't find a solution to my specific problem.
If you could direct me that would be a huge help, thanks!
javascript
xml
xmlhttprequest
0 Answers
Your Answer