背景信息
我正在制作一个通过AJAX调用从数据中获取数据的应用程序。如果成功方法满足要求,它将调用一个函数并将回调参数作为参数传递。然后在for循环中调用该回调。
问题
每当我调用回调函数时,它都会引发错误Uncaught TypeError:回调不是函数(在第39行)
我查找并尝试过的来源
首先让我说我已经对callback参数做了console.log()
并且值正确。
https://developer.mozilla.org/en-US/docs/Glossary/Callback_function
Uncaught TypeError: callback is not a function
add callback to function(e) Uncaught TypeError: callback is not a function
Uncaught TypeError: callback is not a function
我的代码
function Query(eventObject, button, callback)
{
//replaced not relevant code here with a comment.
success: function(response)
{
if (button === "getB")
{
//success fetches correctly. Already checked whether response could be empty. This is not the case
response = JSON.parse(response);
if (callback)
{
//pass the callback along
rollOut(response, callback);
}
}
}
}
//THE FUNCTION I AM TRYING TO INVOKE
function setCheckboxes(valueToCompare)
{
console.log(valueToCompare);
}
function rollOut(jsonResponse, callback)
{
$(`.show-divSet`).on(`click`, function () {
for (let i = 0; i < jsonResponse.length; i++)
{
if (jsonResponse[i]["version"] !== "")
{
//CONSOLE SHOWS ME THE CORRECT VALUE
console.log(callback);
//THIS IS LINE 39. THIS IS WHERE THE ERROR IS THROWN
callback(jsonResponse[i]["version"]);
}
}
});
}
我如何全部实例化代码
$(function () {
// I have already tried setCheckboxes WITHOUT quotes, but unfortunately this didn't work either
Query("", "getB", "setCheckboxes");
});