简体   繁体   中英

Passing data from View to Controller using Ajax results in 404 error on the controller method

I'm trying to pass data from View to Controller Action method. Using Ajax to pass the data to the controller but the controller doesn't receive the data and it is always null resulting in 404 error. Can someone review this and point out what needs to be fixed?

Ajax call in View -

  function openErrorDetails(errors) {

    $.ajax({
        type: 'POST',
        url: "/Home/ErrorDetails",
        dataType: 'json',
        data: JSON.stringify({ errors }),
            success: function (data) {
                var win = window.open();
                win.document.write(data);
            }
        });
    }

Calling the Ajax funtion using a anchor tag OnClick event to open a new window with the errors details -

var exception = "onClick='openErrorDetails(" + JSON.stringify(data) + ")'> View details";

Controller -

    [HttpPost]

    public ActionResult ErrorDetails(string errors)
    {

        if (errors != null)
        {
            dynamic errorMessages = JsonConvert.DeserializeObject(errors);

            return View("ErrorDetails", errorMessages);
        }
        else
        {
            return new HttpNotFoundResult();
        }
    }

这解决了它的数据:{错误:JSON.stringify({错误})},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM