简体   繁体   English

在jquery中获取错误ajax“函数未定义”

[英]Getting error in jquery ajax “function not defined”

I am returning some values from jquery ajax. 我从jquery ajax返回一些值。 I am getting the result but with this I am also getting a error. 我得到了结果,但是我也得到了一个错误。 Please first of all take a look at code 首先请看一下代码

function get_CommentCount(handleData) {
    $.ajax({
        url: 'Profile/get_CommentCount',
        type: "post",
        dataType: 'json',
        success: function(data) {
            handleData(data);
        }
    });
}

calling this function like this 像这样调用这个函数

get_CommentCount(function(output) {
    console.log('output', output)
});

Its give me error that TypeError: handleData is not a function . 它给我一个错误, TypeError: handleData is not a function Please anybody tell me why I am getting this error. 请有人告诉我为什么我收到这个错误。 I have gone through the stackoverflow questions but I am not finding any solution for me. 我已经完成了stackoverflow问题,但我找不到任何解决方案。 May be you find it duplicate but I post this question after surfing the stackoverflow. 可能你发现它重复但我在浏览stackoverflow后发布了这个问题。

Thanks 谢谢

I think the problem here is that you are passing an anonymous function to get_CommentCount. 我认为这里的问题是你将一个匿名函数传递给get_CommentCount。 Try with a normal function, it should work better. 尝试使用正常功能,它应该更好。

In the success function of your ajax you have wrote handle(data), and it is a function that you havent wrote any where may be, so this error is showing you that TypeError: handleData is not a function so you have define a function like 在你的ajax的成功函数中你已经编写了句柄(数据),它是一个你没有写过任何可能的函数,所以这个错误告诉你TypeError: handleData is not a function所以你已经定义了一个像

function handleData(data)
{
    alert(data);
}

and this will works for you. 这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM