Javascript窗口[“函数名”](参数)返回;不是一个函数;打字错误
我正在尝试根据我的应用程序中当前活动的选项卡运行AJAX函数。当我在某些事件后调用函数时,一切正常,但我无法使用字符串变量动态调用函数 回答如下:我刚刚收到一个TypeError,描述我调用的对象不是函数 这是我的ajax函数:Javascript窗口[“函数名”](参数)返回;不是一个函数;打字错误,javascript,jquery,ajax,eval,Javascript,Jquery,Ajax,Eval,我正在尝试根据我的应用程序中当前活动的选项卡运行AJAX函数。当我在某些事件后调用函数时,一切正常,但我无法使用字符串变量动态调用函数 回答如下:我刚刚收到一个TypeError,描述我调用的对象不是函数 这是我的ajax函数: function home_tab_fetchMore(items_count) { let request = new XMLHttpRequest(); let output = [] request.open('GET', `feedstr
function home_tab_fetchMore(items_count) {
let request = new XMLHttpRequest();
let output = []
request.open('GET', `feedstream/${items_count}`);
request.onload = function () {
if (request.status === 200 && request.responseText != '') {
let new_items = JSON.parse(request.responseText);
for (let i = 0; i < new_items.length; i++) {
let item = new_items[i];
let event_id = feed_stream_row.children.length + 1;
let feed_item = htmlToElement(`
<div class="grid-item" id="eventcard_${event_id}">
<div class="card-header">Featured</div>
<div class="card-body">
<h5 class="card-title">${item.fields['title']}</h5>
<p class="card-text">${item.fields['appointment']}
<br>
${dummy_text.sentence(5, 40)}
</p>
</div>
</div>
`);
// appending new item to DOM and updating masonry laytout
feed_stream_row.appendChild(feed_item);
msnry.appended(feed_item);
output.push(feed_item);
}
} else {
console.log('no response!');
}
};
request.send();
return output;
};
TypeError还有一些我无法理解的额外行:
Uncaught TypeError: fn is not a function
at main.js:128
at dispatch (jquery-3.3.1.slim.min.js:2)
at v.handle (jquery-3.3.1.slim.min.js:2)
我感谢您事先提出的任何建议和指导,谢谢
function home_tab_fetchMore(items_count) {}
上述函数定义在窗口
对象中不可用,除非调用它。而是使用函数表达式(在定义函数表达式之前不能使用它们)
上述函数定义在窗口
对象中不可用,除非调用它。而是使用函数表达式(在定义函数表达式之前不能使用它们)
将函数声明替换为函数表达式
home\u tab\u fetchMore=function(items\u count){
@Praveen你是个救生员!真的谢谢你。你为什么不加上这个作为答案,这样我可以选择作为解决方案呢?返回输出;
这行不通,因为async@James它实际上是有效的,我不明白为什么。我以前对这个也很头疼,但不知怎么的它成功了。现在它返回一个数组of html元素用函数表达式替换函数声明home\u tab\u fetchMore=function(items\u count){
@Praveen你是个救生员!真的谢谢你。你为什么不加上这个作为答案,这样我可以选择作为解决方案呢?返回输出;
这行不通,因为async@James它实际上是有效的,我不明白为什么。我以前对这个也很头疼,但不知怎么的它成功了。现在它返回一个数组of html元素
function home_tab_fetchMore(items_count) {}
home_tab_fetchMore = function() {} OR
window.home_tab_fetchMore = function() {}