【问题标题】:AJAX post data is null in controller mvc控制器 mvc 中的 AJAX 发布数据为空
【发布时间】:2014-01-22 06:18:29
【问题描述】:

我尝试将 JSON 对象发送回服务器。这是我的 AJAX 调用:

$.ajax({
    url: '/Home/NewService',
    async: false,
    type: "POST",
    data: JSON.stringify(props),
    error: function (jqXHR, textStatus, errorThrown) {
        console.log("FAIL: " + errorThrown);
    },
    success: function (data, textStatus, jqXHR) {
        console.log("SUCCES");
    }
});

浏览器调试器中JSON.stringify(props)的评估是

"[{"name":"firstName","value":"firstValue"}]"

这是控制器中被调用的方法:

[HttpPost]
public void NewService(dynamic json)
{
    Response.Write(json);
}

我遇到的问题是,上面的 json 变量始终是一个空对象。 success 函数被调用,但是当我调试时 json var 显示为空。

请告诉我我做错了什么。 谢谢。

【问题讨论】:

    标签: javascript jquery ajax asp.net-mvc json


    【解决方案1】:

    我认为您无法按照您尝试的方式绑定到动态类型。您可以尝试创建一个映射数据的类,例如:

    public class Content
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
    

    现在开始行动:

    [HttpPost]
    public ActionResult NewService(Content[] data)
    {
        // sweet !
    }
    

    在你的 js 中,像 Olaf Dietsche 说你需要指定你的 contentType

    var props = [
        { "Name": "firstName", "Value": "firstValue" }, 
        { "Name": "secondName", "Value": "secondValue" }
    ];
    
    $.ajax({
        url: '/Home/NewService',
        contentType: "application/json",
        async: true,
        type: "POST",
        data: JSON.stringify(props),
        error: function (jqXHR, textStatus, errorThrown) {
            console.log("FAIL: " + errorThrown);
        },
        success: function (data, textStatus, jqXHR) {
            console.log("SUCCESS!");
        }
    });
    

    【讨论】:

    • contentType: "application/json", 解决了我的问题...谢谢!
    【解决方案2】:

    根据jQuery.ajax(),默认的内容类型是application/x-www-form-urlencoded。如果要以 JSON 格式发送数据,则必须将其更改为

    $.ajax({
        url: '/Home/NewService',
        contentType: 'application/json',
        ...
    });
    

    【讨论】:

      【解决方案3】:

      使用下面的代码解决这个问题

      Ajax 调用

             function SaveDate() {
              var obj = {};
              obj.ID = '10';
              obj.Name = 'Shafiullah';
      
              $.ajax({
                  url: '/Home/GetData',
                  dataType: "json",
                  type: "Post",
                  contentType: 'application/json',
                  data: JSON.stringify({ ID: obj.ID, Name: obj.Name }),
                  async: true,
                  processData: false,
                  cache: false,
                  success: function (data) {
                      alert(data.id + ' , ' + data.name);
                  },
                  error: function (xhr) {
                      alert('error');
                  }
              });
          }
      

      我的控制器动作方法

              [HttpPost]
              public IActionResult GetData([FromBody] Employee employee)
              {
                  return Json(employee);
              }
      

      【讨论】:

      • 这个 5 岁的孩子已经得到了一个赞成的、接受的答案。您能否在答案中添加一些文字,解释这对接受的答案有何改进?
      • 我没有太多时间,但是使用它就可以正常工作
      猜你喜欢
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 2021-02-20
      相关资源
      最近更新 更多