I don't want to seem like I haven't done my research. I was using this as an example: Pass arguments into ajax onreadystatechange callback?
However, I'm afraid my problem is with defining the new function. I was not sure how to even search for this problem, so I decided to ask instead.
Thanks.
The following is saying that doSomething is not a function when I call getOwnerList(). The goal is to have getData() be reusable, and to pass any function I want to act upon the data.
function updateOwnerList(data) {
alert(data);
return;
}
function getOwnerList() {
getData(updateOwnerList, "getOwnerList.php");
}
function getData(doSomething, url) {
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
console.log(data);
doSomething(data);
}
}
ajax.open("GET", "getOwnerList.php", true);
ajax.send();
}
Edit: Actually, the response does come as expected. When I print the data, it is what I wanted it to be. The problem is when I call doSomething() inside or outside of the onreadystatechange function, it says that the function doSomething() does not exist.