I encountered an error in my code, but I managed to resolve it independently. Could someone please provide an explanation of why the code wasn't working and delve into the mechanics behind the issue?
Here is the code snippet:
var listTables = function (tables) {
console.log(tables);
}
var ajaxReq = function (success_cb, url) {
// success_cb can be invoked here without errors
var http = new XMLHttpRequest();
http.onreadystatechange = function (success_cb) {
if (this.readyState == 4 && this.status == 200) {
success_cb(this); // Uncaught TypeError: success_cb is not a function
}
}
http.open("GET", url, true);
http.send();
}
ajaxReq(listTables, "http://www.someurl.com");
Upon removing the success_cb
callback from the onreadystatechange
function definition, the code ran smoothly. Is this issue related to scope?