为什么 ASP.NET MVC 中未定义 Ajax 脚本?

Why is an Ajax script not defined in ASP.NET MVC?

提问人:Utility Monster 提问时间:9/3/2023 最后编辑:Utility Monster 更新时间:9/3/2023 访问量:28

问:

我在 .NET 4.7.2 Web 应用程序上将 tinymce 富文本编辑器添加到我的 ASP.NET MVC 中。

问题是:由于防伪块 ASP.NET 我无法发布富文本内容。

因此,我尝试使用 AJAX 将 AF 令牌放入请求标头中,并将其与后端生成的令牌进行比较,但它不起作用。生成,但不执行脚本。Jquery 3.7.0.js 被包含并呈现。 Firefox调试控制台告诉我:“$未定义”。AntiforgeryCookie

有什么想法吗?

@model Blog.Data.Posts

@{
    ViewBag.Title = "Create";
    if (User.IsInRole("Admin"))
    {
        Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
    }
    else
    {
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    string token = TokenHeaderValue();
}
<head>
    <script>
        tinymce.init({
            selector: 'textarea'
        });
    </script>
</head>

@using (Html.BeginForm(FormMethod.Post))
{
    //creates AF hidden field
    @Html.AntiForgeryToken()
    //TODO:
    //put the value in the header!

    @functions
    {
        public string TokenHeaderValue()
        {
            string cookieToken, formToken;
            AntiForgery.GetTokens(null, out cookieToken, out formToken);

            return cookieToken + ":" + formToken;
        }
    }

<script>
    $(document).ready(function () {
        ajax({
            url: "~/Posts/Create/",
            type: "post",
            contentType: "application/json",
            data: {}, // JSON data goes here
            dataType: "json",
            headers: {
                'RequestVerificationToken': '@token'
            }
        });
    });
</script>


    <div class="form-horizontal">
        <h4>Beitrag erstellen</h4>
        <hr />
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-10">
                <textarea name="Text"></textarea>
                @Html.ValidationMessageFor(model => model.Text, "", new { @class = "text-danger" })
            </div>
        </div>

        <input type="hidden" name="UserId" value="@ViewBag.UserId" />

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </div>
    </div>
}

<div>
    @if (User.IsInRole("Admin"))
    {
        @Html.ActionLink("Back to List", "AdminMainMenu")
    }
    else
    {
        @Html.ActionLink("Back to List", "Index", "Users")
    }
</div>

<!--Der Inhalt wird vom _Layout/_LayoutAdmin gerendert. -->
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

enter image description here

ASP.NET-AJAX ANTIFORGERYTOKEN

评论

0赞 Mark Seemann 9/3/2023
请不要上传代码/数据/错误的图片。

答: 暂无答案