繁体   English   中英

ASP.NET Core 2.1 MVC 使用 XMLHttpRequest 将数据从 JavaScript 发送到 Action 方法

[英]ASP.NET Core 2.1 MVC send data from JavaScript to Action method using XMLHttpRequest

这与下面类似,但没有 Ajax。 我正在使用 JavaScript 和 XMLHttpRequest

AJAX 发布数据到达 ASP.NET Core 2.1 控制器时为空

在 ASP.NET MVC 中一切正常,但我正在学习 ASP.NET Core MVC。

Home.cshtml 中的按钮调用下面的 JavaScript 方法,该方法会调用 HomeController.cs 中名为 Test 的方法。

我的问题是如果我在我的测试方法中保留断点 serverName 和 port 都是空的

function MyMethod() {
    var xmlhttp = new XMLHttpRequest();
    var url = "/Home/Test";
    var input = {};
    input.serverName = document.getElementById("serverName").value;
    input.port = document.getElementById("port").value;
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var jsResp = JSON.parse(xmlhttp.responseText);
            if (jsResp.Status == "Success") {
                //show success
            }
            else {
                //show error
            }
        }
    }
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    xmlhttp.send(JSON.stringify(input));
}



[HttpPost]
 public JsonResult Test(string serverName, string port)
 {
   try
     {
        if (string.IsNullOrEmpty(serverName) ||
            string.IsNullOrEmpty(port))
         {
            return Json(new { Status = "Error", Message = "Missing Data" });
         }
        else 
         {
        return Json(new { Status = "Success", Message = "Got data" });
         }
       }
       catch (Exception e)
        {
             return Json(new { Status = "Error", Message = e.Message });
        }
 }

我什至在下面尝试过,但没有任何帮助

public JsonResult Test(JObject serverName, JObject port) -- controller method not hiting

public JsonResult Test(object serverName, object port) -- not allowing me to cast into string

public JsonResult Test([FromBody] string serverName, [FromBody] string port)

由于您的内容类型是application/json;charset=UTF-8 ,您需要使用[FromBody]并根据您的情况将数据作为对象接收。

此外,您只能在动作参数中使用[FromBody]一次(来自此处

不要将 [FromBody] 应用于每个操作方法的多个参数。 ASP.NET Core 运行时将读取请求流的责任委托给输入格式化程序。 一旦请求流被读取,就不能再被读取以绑定其他 [FromBody] 参数。

您可以按照以下步骤正确传递数据:

1.创建一个视图模型:

public class ServerModel
{
    public string serverName { get; set; }
    public string port { get; set; }
}

2.动作:

[HttpPost]
    public JsonResult Test([FromBody] ServerModel data)
    {
        try
        {
            if (string.IsNullOrEmpty(data.serverName) ||
                string.IsNullOrEmpty(data.port))
            {
                return Json(new { Status = "Error", Message = "Missing Data" });
            }
            else
            {
                return Json(new { Status = "Success", Message = "Got data" });
            }
        }
        catch (Exception e)
        {
            return Json(new { Status = "Error", Message = e.Message });
        }
    }

暂无
暂无

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

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