Uncaught ReferenceError:未定义“函数”

问题描述 投票:0回答:1

所以我试图让一个非常简单的ajax调用正常工作,但总是会遇到上述错误。

视图:(@ model ...在布局上方)

    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")

        <script>
                function DownloadPDF() {
                    $.ajax({
                        type: "POST",
                        url: "/Editor/MailMergeVorlage",
                        data: { tmpGutschein = @Model }
                    })
            };
        </script>
    </head>
    ...
    <body>
    ...
        <section>
                <a href="#!" onclick="DownloadPDF()">
                   <img src="~/Content/images/Icon_Word.png" style="max-width:64px;max-height:64px;" />
                </a>
        </section>
    </body>

控制器:

public void MailMergeVorlage(DefType tmpGutschein)
        {
            Editor editor = new Editor();
            editor.CreatePDF(tmpGutschein);
        }

我尝试过的事情:

  • 将javascript放入.ready函数中
  • 在modernizr脚本下添加@Scripts.Render("~/bundles/jquery")
html asp.net-mvc asp.net-ajax
1个回答
0
投票

首先,您需要在标题标签中添加jquery脚本。写入后,body标签中的函数不在header标签中,如下代码

  @{ Layout = null; }
    <!DOCTYPE html>
    <html>

    <head>
      @Styles.Render("~/Content/css") 
      @Scripts.Render("~/bundles/modernizr") 
      @Scripts.Render("~/bundles/jquery")

    </head>
    ...

    <body>
      ...
      <section>
        <a href="#" onclick="DownloadPDF()">
          <img src="~/Content/images/Icon_Word.png" style="max-width:64px;max-height:64px;" />
        </a>
      </section>
      <script>
        function DownloadPDF() {
          $.ajax({
            type: "POST",
            url: "/Editor/MailMergeVorlage",
            data: @Html.Raw(Json.Encode(Model));
            sucess:function(result){
               alert(result);
             }
          })
        };
      </script>
    </body>
© www.soinside.com 2019 - 2024. All rights reserved.