Ajax Post将null传递给Controller [英] Ajax Post passing null to Controller

查看:208
本文介绍了Ajax Post将null传递给Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向MVC控制器发出AJAX POST请求,但数据以null的形式通过.我不确定为什么.

I am making an AJAX POST request to an MVC controller, yet the data is coming through as null. I'm not sure why.

$(document).ready(function() {
  $("#btnSaveCustomer").click(function() {
    debugger; 
    var data = {
      _DcLink: "1",
      _Account: "Test",
      _Name: "TestName",
      _Title: "Mr",   
      _Init: "T"
    }

    $.ajax({
      method: "POST",
      url: '/Customer/SaveCustomer',
      data: JSON.stringify(data),
      success: function() {
        debugger;
        console.log(data)
        alert("Success")
      },
      error: function() {
        alert("Error")
      }
    });
})

public ActionResult SaveCustomer(string data)
{
  using (var ms = new MemoryStream(Encoding.UTF32.GetBytes(data)))
  {
    // Deserialization from JSON  
    DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Customer));
    Customer serializeddata = (Customer)deserializer.ReadObject(ms);
  }
  return Json(new { Success = true });
}

无论我如何尝试序列化数据,它始终为null.我相信AJAX POST方法无法正确执行

No matter how I try to serialize the data it is always null. I believe the AJAX POST method is not executing properly

我正在使用虚拟数据来解决问题.

I am using dummy data in order to resolve the problem.

代码在控制器中的MemoryStream处遇到断点-声明数据为null.

The code hits a breakpoint at MemoryStream in the controller - stating data is null.

System.ArgumentNullException:'字符串引用未设置为String的实例. 参数名称:s'

System.ArgumentNullException: 'String reference not set to an instance of a String. Parameter name: s'

任何帮助表示赞赏

推荐答案

像这样更改;

data: {data : JSON.stringify(data)},

这篇关于Ajax Post将null传递给Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆