【问题标题】:jQuery 1.5.1 ajax JSON data is null on controllerjQuery 1.5.1 ajax JSON 数据在控制器上为空
【发布时间】:2011-03-16 06:22:12
【问题描述】:

我的 ajax 调用看起来像这样:

  $.ajax({
        type: "POST",
        url: "/Home/GenerateExportReport",
        data: "{ 'fromDate': '2004-12-01', 'toDate': '2011-12-01', 'requestorId': = '1'}",
        async: false,
        cache: false,
        dataType: "json",
        //contentType: 'application/json; charset=utf-8',
        success: function (data) {
            alert("success");
        },

        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown + " text:" + textStatus + "request " + XMLHttpRequest);
        }
    });

我的控制器:

[HttpPost]
public ActionResult GenerateExportReport(string data)
{
  . . .
}

调用正常,但由于某种原因,控制器没有收到 JSON 数据,string data 在每次调用时都为 NULL。

我确实从这个链接尝试了一些修复

从该链接修复jQuery.ajaxSetup() JQuery 1.5 and new "Text JSON" datatype 没有帮助

修复 contentType:"application/json; charset=utf-8",来自该链接 Sending String Data to MVC Controller using jQuery $.ajax() and $.post() 也没有用,当我设置 contentType: "application/json; charset=utf-8" 时,我收到错误代码 500 内部服务器错误

正如其中一篇帖子 jQuery 1.5.1 breaks all ajax() calls 中提到的那样,它可能是 jQuery validation plugin,所以目前我确实从我的页面中删除了对该脚本的引用。

有什么想法吗?

附言 数据中的字符串 ({ 'fromDate': '2004-12-01', 'toDate' ... }) 只是一个示例,我有非常大的 JSON 字符串需要传递给控制器​​:

["ONE", "0", "1", "0", "0", "0", "0", "0", "0", "1", "TWO", "281", "5174", "70", "3406", "1405", "300", "4632", "1522", "16790", "TREE", "13", "174", "4", "119", "32", "18", "94", "45", "499", "FOUR", "28", "931", "17", "755", "414", "17", "1138", "353", "3653", "FIVE", "2", "30", "0", "12", "8", "0", "12", "3", "67", "SIX", "13", "250", "7", "173", "77", "18", "247", "49", "834", "9am", "0", "2", "0", "0", "0", "0", "1", "1", "4", "SEVEN", "185", "2838", "45", "2100", "828", "314", "2324", "1223", "9857", "EIGHT", "173", "3662", "23", "1798", "612", "95", "2007", "445", "8815", "NINE", "308", "5277", "52", "3800", "1842", "154", "5548", "1910", "18891", "TEN", "17", "233", "3", "145", "69", "21", "199", "70", "757", "Total", "1020", "18572", "221", "12308", "5287", "937", "16202", "5621", "60168"]

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您不是输出中名为“数据”的字段的值,您只是为输出设置了一个字符串。要使其显示,请将您的数据值更改为:

    data: { data: "{ 'fromDate': '2004-12-01', 'toDate': '2011-12-01', 'requestorId': = '1'}" },
    

    虽然这很糟糕。我认为你应该这样做:

    data: { fromDate: '2004-12-01', toDate: '2011-12-01', requestorId: 1}
    

    然后

    [HttpPost]
    public ActionResult GenerateExportReport(DateTime fromDate, DateTime toDate, int requestorId)
    {
        ...
    }
    

    【讨论】:

      【解决方案2】:

      默认情况下,ASP.NET MVC 不将操作参数视为 JSON。如果使用 ASP.NET MVC 3,您可以尝试 JsonValueProviderFactory:

      将此代码放入 Global.asax 中进行注册:

      ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
      

      或者使用常规参数:

      $.ajax({
          type: "POST",
          url: "/Reports/GenerateExportReport",
          data: {fromDate: '2004-12-01', toDate: '2011-12-01', requestorId: 1}
          /* rest */
      });
      
      [HttpPost]
      public ActionResult GenerateExportReport(DateTime fromDate, DateTime toDate, int requestorId)
      {
         /* implementation */
      }
      

      【讨论】:

        【解决方案3】:

        请检查以下代码是否适合您:

        var param = {};
        param.fromDate = '2004-12-01';
        param.toDate = '2011-12-01';
        param.requestorId = '1';
        
        $.ajax({
                        type: "POST",
                        url: "/Home/GenerateExportReport",
                        data: param,
                        async: false,
                        cache: false,
                        dataType: "json",
                        //contentType: 'application/json; charset=utf-8',
                        success: function (data) {
                            alert("success");
                        },
        
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(errorThrown + " text:" + textStatus + "request " + XMLHttpRequest);
                        }
                    });
        

        控制器中的代码

            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult GenerateExportReport()
            {
            //Code to get the data
            Request.Form["fromDate"]
            Request.Form["toDate"]
            Request.Form["requestorId"]
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多