简体   繁体   English

Javascript Ajax回调函数未定义错误

[英]Javascript Ajax Callback function not defined error

I tried searching all around since this seems to me a very common (NuBee?) problem, but nothing worked for me...so here it is 我尝试到处搜索,因为在我看来这是一个非常常见的(NuBee?)问题,但对我没有任何帮助...所以在这里

To cut right to the chase, my Razor code (shown later) posts an ajax form to a MVC4 action which works fine but any attempt to add a callback function to the form ( OnSuccess , OnBegin etc) fails with a runtime error that the callback function is not defined. 为了切入正题,我的Razor代码(稍后显示)将ajax表单发布到MVC4操作中,该操作可以正常工作,但是任何向该表单添加回调函数的尝试( OnSuccessOnBegin等)都会失败,并出现运行时错误,即回调函数未定义。 This is the image of the error message in the my Chrome browser, right after the exception occurs: 这是发生异常后立即在我的Chrome浏览器中显示的错误消息的图像:

JSChromeError

This is the form's code: 这是表单的代码:

    @using (Ajax.BeginForm("AskQuestion", null, 
        new AjaxOptions() { 
            OnSuccess = "endAskQuestion" },
        new { id = "askQuestionForm" }))
    {
        @Html.AntiForgeryToken() 
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>@ViewBag.Controller</legend>
            <br />
            @{ var investigationID = Model.Investigation.InvestigationID; }
            @Html.HiddenFor(model => investigationID)
            <input type="submit" class="button" value="שלח היגד"/>
            <input type="button" id="cancelEdit" class="button" value="בטל עריכה"/>
        </fieldset>
    }

And the script inside which the endAskQuestion is defined: 还有定义endAskQuestion的脚本:

$(document).ready(function () {

    //deleted several Jquery and JS functions...

    // callback function definition
    function endAskQuestion() {
        alert("adad");
    }
})

Also, I saw some posts that stated the references to JQuery and Ajax scripts are important so here's the html's head section with the references: 另外,我看到一些帖子指出对JQuery和Ajax脚本的引用很重要,因此这是带有引用的html头部分:

<head>
    <meta charset="utf-8" />
    <title>RTInvestigation</title>
    <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <link href="/Content/css?v=_WuF_JGHabtqdWNcwICWILVG9A391eZyF0GO24jlq9U1" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" />
    <script> </script>
    <meta name="viewport" content="width=device-width" />
    <script src="/Scripts/jquery-1.8.2.js"></script>

    <script src="/Scripts/jquery-ui-1.8.18.js"></script>
    <script src="/Scripts/jquery-ui-1.8.20.js"></script>

    <script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
    <script src="/Scripts/jquery.validate.js"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.js"></script>

</head>

I don't know why Razor creates references for two Jquery-UI versions...I couldn't find the reason. 我不知道为什么Razor为两个Jquery-UI版本创建引用...我找不到原因。

How come the callback function is not defined?? 为什么不定义回调函数?

Thanks for helping 感谢您的帮助

Take endAskQuestion out of the $(document).ready function. $(document).ready函数中取出endAskQuestion Also, use a semicolon at the end of each javascript code line, as that will often throw undefined errors, as well. 另外,请在每条javascript代码行的末尾使用分号,因为这也会经常引发未定义的错误。

$(document).ready(function () {

    //deleted several Jquery and JS functions...

}); // added semicolon here...

// callback function definition should be moved outside
function endAskQuestion() {
    alert("adad");
}

The scope of your endAskQuestion is limited to the anonymous function that jQuery calls when the document is ready. endAskQuestion的范围限于文档准备好后jQuery调用的匿名函数。 As ps2goat says, you need to take it out of the $(document).ready callback. 正如ps2goat所说,您需要将其从$(document).ready回调中删除。 In this way it will be defined globally and will be accesible through closure to the callbakc you're using for AJAX. 这样,它将被全局定义,并且可以通过关闭用于AJAX的callbakc来访问。 Read about scoping, closures and anonymou. 阅读有关范围,闭包和匿名用户的信息。

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

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