未捕获的类型错误:undefined是不是一个函数错误、类型、一个函数、undefined

2023-09-10 20:40:09 作者:寂寞叹红颜

我得到的消息未捕获的类型错误:undefined是不是一个函数当我尝试在我家的控制器调用一个方法

建议也许是为什么我得到这个消息?

  findIdpActivities =功能(PERNR,回调){
    restEndPoint = ServiceBase的+'主页/ FindIdpActivities;
    数据={'PERNR':'+ PERNR +'};
    makeJsonDataAjaxCall(回调);
};

makeJsonDataAjaxCall =函数(回调,OBJ){
    $阿贾克斯({
        键入:POST,
        网址:restEndPoint,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        数据:数据,
        成功:功能(数据){
            回调(数据);
        }
    });
};
 

在按钮单击执行方法。

  $(文件)。在(点击,输入[名称= btnViewActivities]功能(E){
    即preventDefault();
    。VAR值= $(本).parent()找到(输入[名称= hiddenPerNr])VAL()。
    dataService.findIdpActivities(值);
});
 

和这是在HomeController的方法

  [HttpPost]
    公共JsonResult FindIdpActivities(字符串PERNR)
    {
        viewModel.GetIdpActivities(PERNR);

        返回JSON(新
            {
                活动= viewModel.IdpActivities
            });
    }
 
JavaScrip 4 JavaScript 的数据类型

解决方案

这就是当你试图调用一个函数被定义在此之前发生的共同JavaScript错误。

例如,虽然这code按预期工作:

  VAR的sayHello =功能(){
    返回你好!
};

警报(sayHello的());
 

如果您反转语句的顺序中的未捕获的类型错误:未定义不是一个函数的会发生错误:

 警报(的sayHello());

VAR的sayHello =功能(){
    返回你好!
};
 

因此​​,我建议你仔细检查正在正确加载脚本,并在 findIdpActivities 被正确初始化为的DataService 对象的功能。

I get the message Uncaught TypeError:Undefined is not a function when I try to call a the method in my home controller.

Advice perhaps as to why I am getting this message?

findIdpActivities = function (pernr, callback) {
    restEndPoint = serviceBase + 'Home/FindIdpActivities';
    data = "{'perNr':'" + pernr + "'}";
    makeJsonDataAjaxCall(callback);
};

makeJsonDataAjaxCall = function (callback, obj) {
    $.ajax({
        type: "POST",
        url: restEndPoint,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: data,
        success: function (data) {
            callback(data);
        }
    });
};

executing the method upon button click.

$(document).on("click", "input[name=btnViewActivities]", function (e) {
    e.preventDefault();
    var value = $(this).parent().find("input[name=hiddenPerNr]").val();
    dataService.findIdpActivities(value);
});

and this is the method in the HomeController

[HttpPost]
    public JsonResult FindIdpActivities(string perNr)
    {
        viewModel.GetIdpActivities(perNr);

        return Json(new
            {
                Activities = viewModel.IdpActivities
            });
    }

解决方案

That's a common javascript error that happens when you try to call a function before it is defined.

For instance, while this code works as expected:

var sayHello= function () {
    return 'Hello!'
};

alert(sayHello());

If you reverse the order of the statements the "Uncaught TypeError: undefined is not a function" error will occur:

alert(sayHello());

var sayHello= function () {
    return 'Hello!'
};

Hence, I suggest you double check that your scripts are being loaded correctly, and that the findIdpActivities is being properly initialized as a function of the dataService object.

 
精彩推荐
图片推荐