I have encountered an issue where I am passing a callback function but unable to call it when the onreadystatechange changes its value, specifically
request.onreadystatechange = func
. Even though I receive a response from the server when making the ajax request, the function func
is not being called. It's important to note that func
is passed as a string parameter when calling getFromServer("http://localhost/ch02/checkName.php?username=sas","func")
function createRequest() {
try {
request = new XMLHttpRequest();
}
catch (failed) {
request = null;
}
finally {
return request;
}
}
function func(){
alert("ok");
}
function getFromServer(url, readystateCallback) {
request=createRequest();
if (request == null) {
alert("unable to create request");
} else {
request.open("GET", url, true);
var func= new Function(readystateCallback);
request.onreadystatechange = func;
request.send(null);
}
return request;
}