0

i want to access jquery ajax post request body to get the value on my asp.net mvc 5 controller action. knowing that i m passing __RequestVerificationToken with success.

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

           });});

here is my controller

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

i have a hidden form on my view

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

the body content is
name|test
__RequestVerificationToken|the token

1 Answer 1

0

Fixed my problém was that ($(this).data('url')) are not giving the correct url because it's not reffering to $("#renamedashboard") who has the correct url

    <a id="renamedashboard" href="#" [email protected]("Rename", "Dashboard",new {id=activedashboard.Idd}) >
Sign up to request clarification or add additional context in comments.

1 Comment

I don't undestand what you said so -1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.