POST 500 (Internal Server Error) ajax,mvc

后端 未结 2 1659
一个人的身影
一个人的身影 2021-01-14 00:34

I have ajax request that sends data to my controller , it collects value of my dropdown

the error is

POST http://localhost:65070/form/create 500 (Int         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 01:37

    Your post method must have the [ValidateAntiForgeryToken] attribute. Either remove the attribute or in the view, add the token

    @Html.AntiForgeryToken()
    

    and pass it back in the ajax function

    $('#State').change(function () {
      var a = $('#State').val();
      var token = $('[name=__RequestVerificationToken]').val();
      $.ajax({
        ....
        data: { __RequestVerificationToken: token, 'SubID': a },
        ....
    

    Note the form parameter is not necessary in the action method

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(int SubID)
    {
      ....
    

提交回复
热议问题