javascript 未捕获的类型错误:回调不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30099128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:40:06  来源:igfitidea点击:

Uncaught TypeError: callback is not a function

javascriptjqueryajax

提问by jagadeesh puthukkudi

I have a function:

我有一个功能:

reportAdminActions.reportMemberList(project, function(data) {
    console.log(data);
}); 

This function is called by another ajax operation like these:

此函数由另一个 ajax 操作调用,如下所示:

reportMemberList: function(projectId, callback) {
    var projectDetail = new Object();
    projectDetail.projectId = projectId;
    var pluginArrayProject = new Array();
    pluginArrayProject.push(projectDetail);   
    $.ajax({
        url : ConfigCom.serverUrl + 'projectreportonmember',
        dataType: "jsonp",
        type: "POST",
        data: JSON.stringify(pluginArrayProject)
    }).always(function(data) {
        callback(data.responseText);
    });
}

I need return value to function defined area after ajax operation. But here I got a error

我需要在 ajax 操作后返回函数定义区域的值。但在这里我得到了一个错误

Uncaught TypeError: callback is not a function

回答by Hoosh

Check the rest of your code for calls to reportMemberListand make sure you always call it with the callback as a parameter. If you omit the callback parameter anywhere (e.g. call reportMemberListwith just the projectIdparameter), the code above would parse correctly the other calls to the function with the callback would produce the error. (This was the solution for me.)

检查其余代码是否调用reportMemberList并确保始终使用回调作为参数调用它。如果您在任何地方省略回调参数(例如reportMemberList,仅使用projectId参数调用),上面的代码将正确解析对具有回调的函数的其他调用将产生错误。(这对我来说是解决方案。)

回答by smnbbrv

guessing, but try to change your "jsonp" to "json". If you don't make cross-origin requests there, it should work

猜测,但尝试将您的“jsonp”更改为“json”。如果你不在那里提出跨域请求,它应该可以工作

reportMemberList: function(projectId, callback) {
    var projectDetail = new Object();
    projectDetail.projectId = projectId;
    var pluginArrayProject = new Array();
    pluginArrayProject.push(projectDetail);   
    $.ajax({
        url : ConfigCom.serverUrl + 'projectreportonmember',
        dataType: "json",
        type: "POST",
        data: JSON.stringify(pluginArrayProject)
    }).always(function(data) {
        callback(data.responseText);
    });
}