POST 500 (Internal Server Error) ajax,mvc

后端 未结 2 1657
一个人的身影
一个人的身影 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:21

    I have an example that I have used in my applications. In the controller, you must place

    [HttpPost]
    [ValidateAntiForgeryToken]
        public JsonResult Verificar(int idempleado, string desde, string hasta)
         {
          ...
         return Json(suiche, JsonRequestBehavior.AllowGet);
         }
    

    In the view, I have an ajax function

        function Verificar(desde, hasta) {
            var token = $('[name=__RequestVerificationToken]').val();
            var empleadoId = parseInt($('#empleadoId').val());
            var url = '/Settlements/Verificar';
            var settings = {
                "async": true,
                "crossDomain": true,
                "url": url,
                "method": "POST",
                "data": { __RequestVerificationToken: token, idempleado: empleadoId, desde: desde, hasta: hasta, }
            }
            $.ajax(settings).done(function (response) {
                if (response) {
                    $('#mensajefrom').text(null);
                    $('#mensajeto').text(null);
                } else {
                    $('#mensajefrom').text("There is another settlement with that date range");
                    $('#mensajeto').text("There is another settlement with that date range");
                }
            });
        }
    

    in the form, I use @Html.AntiForgeryToken()

    0 讨论(0)
  • 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)
    {
      ....
    
    0 讨论(0)
提交回复
热议问题