Let's consider we have a file named hello.txt, and its content is :
Hello World !
We have a constant res, which is defined as :
const res = AJAXResponse("hello.txt")
So, far I defined AJAXResponse() as :
const AJAXResponse = (src) => {
var request = new XMLHttpRequest();
request.open('GET', src, true);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
// Success!
var resp = this.response;
} else {
// We reached our target server, but it returned an error
}
}
request.onerror = function() {
// There was a connection error of some sort
}
request.send()
}
This works preety fine but I am not able to attach the data in this.response to the res constant. I want the output to be something like this :
const res = "Hello World !" // As returned by the function
But, I don't know how to do it. Can you help me with that ?