N
news
Hi. I hope someone can help me with this ^^
I'm having some trouble understanding how I can define my own function
to be run (not in-line) when an AJAX call is done without having to
keep a. Have I understood correctly that
XmlHttpRequest.onreadystatechange is a function within the
XmlHttpRequest class that one "overrides" when using ajax? If so, why
does not the this-keyword work within this function? (Like when
defining your own classes). I thought maybe the garbage collection
messed it up, but I tried defining the varable in global scope and I
still got the same result. I'll try to illustrate what I'm trying to
do below.
function processResponse() {
if(this.readyState == 4) { //this.readyState is undefined!
//...
}
}
function getRequest() {
//...
}
function sendRequest(responseFunction, parameters) {
var request = getRequest();
request.onreadystatechange = responseFunction;
request.open("POST", "url", true);
request.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
request.send(parameters);
}
sendRequest(processResponse, "");
---
When defining a class it works wonderfully:
function MyClass() {
this.x = 5;
this.getX = myClassGetX;
}
function myClassGetX() {
return this.x;
}
I'm having some trouble understanding how I can define my own function
to be run (not in-line) when an AJAX call is done without having to
keep a. Have I understood correctly that
XmlHttpRequest.onreadystatechange is a function within the
XmlHttpRequest class that one "overrides" when using ajax? If so, why
does not the this-keyword work within this function? (Like when
defining your own classes). I thought maybe the garbage collection
messed it up, but I tried defining the varable in global scope and I
still got the same result. I'll try to illustrate what I'm trying to
do below.
function processResponse() {
if(this.readyState == 4) { //this.readyState is undefined!
//...
}
}
function getRequest() {
//...
}
function sendRequest(responseFunction, parameters) {
var request = getRequest();
request.onreadystatechange = responseFunction;
request.open("POST", "url", true);
request.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
request.send(parameters);
}
sendRequest(processResponse, "");
---
When defining a class it works wonderfully:
function MyClass() {
this.x = 5;
this.getX = myClassGetX;
}
function myClassGetX() {
return this.x;
}