0

我想访问 jquery ajax 发布请求正文以获取我的 asp.net mvc 5 控制器操作的值。知道我成功传递了 __RequestVerificationToken。

<script>
$(document).ready(
       function () {
           $("#renamedashboard").click(function () {
               swal({
                   title: "Titre Dashboard",
                   text: "Saisir le nouveau titre:",
                   type: "input",
                   showCancelButton: true,
                   closeOnConfirm: false,
                   animation: "slide-from-top",
                   inputType : "text"
               },
               function (inputValue)
               {
                   if (inputValue === false)
                       return false;
                   if (inputValue === "")
                   {
                       swal.showInputError("You need to write something!");
                       return false
                   }
                   alert(inputValue);
                   var form = $('#__AjaxAntiForgeryForm');
                   //form.append('<input type="hidden" name="name" value=' + inputValue + ' />');
                   var token = $('input[name="__RequestVerificationToken"]', form).val();
                   $.ajax({
                       url: ($(this).data('url')),// i tried to do ($(this).data('url'))+'?name='+inputValue but i got undefined id
                       type: "POST",
                       data: {
                           name: inputValue,
                           __RequestVerificationToken: token
                       },
                       success: SuccessCallback,
                       error: FailureCallback
                   });
               });
               function SuccessCallback(data) {
                   swal({
                       title: "Opération réussie",
                       type: "success"

                   }, function () {
                       NProgress.done();
                       location.reload();
                   });
               }
               function FailureCallback(data) {
                   swal("Good job!", "You clicked the button!", "error");
                   NProgress.done();
               }

           });});

这是我的控制器

        [HttpPost]
    [ValidateAntiForgeryToken]
    // POST: Dashboard/Rename/5
    public async Task<ActionResult> Rename(int id,[System.Web.Http.FromBody] string name)
    {
        Dashboard dashboard = await dbs.Dashboards.FindAsync(id);
        //dashboard.Visible = true;
        dashboard.TitreD = name.ToString();
        dbs.Entry(dashboard).State = EntityState.Modified;
        await dbs.SaveChangesAsync();
        return Json(new { success = true });
    }

我的视图上有一个隐藏的表格

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "__AjaxAntiForgeryForm" })){
@Html.AntiForgeryToken()
}

正文内容为
name|test
__RequestVerificationToken|token

4

1 回答 1

0

修复了我的问题是 ($(this).data('url'))没有给出正确的 url,因为它没有提到$("#renamedashboard")谁拥有正确的 url

    <a id="renamedashboard" href="#" data-url=@Url.Action("Rename", "Dashboard",new {id=activedashboard.Idd}) >
于 2015-04-13T20:26:44.120 回答