Uncaught TypeError:Undefined is not a function

2019-05-27 16:33发布

问题:

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
            });
    }

回答1:

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.