简体   繁体   English

使用 Ajax 将数据从 View 传递到 Controller 导致控制器方法出现 404 错误

[英]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.我正在尝试将数据从 View 传递到 Controller Action 方法。 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.使用 Ajax 将数据传递给控制器​​,但控制器不接收数据并且它始终为 null,从而导致 404 错误。 Can someone review this and point out what needs to be fixed?有人可以查看此内容并指出需要修复的内容吗?

Ajax call in View -视图中的 Ajax 调用 -

  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 -使用锚标记 OnClick 事件调用 Ajax 函数以打开一个包含错误详细信息的新窗口 -

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

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({错误})},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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