最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: callback is not a function - Stack Overflow

programmeradmin1浏览0评论

I have a function:

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

This function is called by another ajax operation like these:

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

Uncaught TypeError: callback is not a function

I have a function:

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

This function is called by another ajax operation like these:

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

Uncaught TypeError: callback is not a function
Share Improve this question edited May 7, 2015 at 11:02 Regent 5,1763 gold badges23 silver badges35 bronze badges asked May 7, 2015 at 10:58 jagadeesh puthukkudijagadeesh puthukkudi 931 gold badge1 silver badge8 bronze badges 4
  • I am not sure but i doubt it is because of your JSONP – Vigneswaran Marimuthu Commented May 7, 2015 at 11:15
  • Bad luck. It is not working when i removed JSONP. – jagadeesh puthukkudi Commented May 7, 2015 at 11:18
  • could you please post the sample of your server response? – smnbbrv Commented May 7, 2015 at 11:20
  • It is JSON Response [{"timesheet_user_id":"5","timesheet_hours":"4","name":"jagadeesh puthukkudi"},{"timesheet_user_id":"8","timesheet_hours":"7","name":"admin admin"},{"timesheet_user_id":"5","timesheet_hours":"2","name":"jagadeesh puthukkudi"}] – jagadeesh puthukkudi Commented May 7, 2015 at 11:32
Add a ment  | 

2 Answers 2

Reset to default 1

Check the rest of your code for calls to reportMemberList and make sure you always call it with the callback as a parameter. If you omit the callback parameter anywhere (e.g. call reportMemberList with just the projectId parameter), 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.)

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

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);
    });
}
发布评论

评论列表(0)

  1. 暂无评论