2014-09-02 47 views
1

我有一个js功能:行动收到一个空字符串后,阿贾克斯后

的Javascript:

function Post() 
{ 
    var table = $('#table4').dataTable(); 
    var data = table.$('input:text').serialize(); 
    console.log(data); 

    $.ajax({ 
     type: "POST", 
     url: '@Url.Action("SaveList")', 
     data: JSON.stringify(data), 
     dataType: "json", 
     contentType: 'application/json', 
     success: function() { 
      alert('success'); 
     }, 
     error: function() { 
      alert('error'); 
     } 

    }); 
} 

操作:

[HttpPost] 
    public ActionResult SaveList(string serializedString) 
    { 
     var a = serializedString; 
     return RedirectToAction("CustomersList"); 
    } 

的问题是行动收到空蜇console.log(data)显示,有内容,如果我把断点在控制器上停止,但serializedString为空。哪里可能是一个问题?谢谢!

+0

尝试'数据类型:'text''和删除'contentType'。 – 2014-09-02 10:00:21

+0

@FlorianGl - 说'dataType:“文本”'不会帮助。问题是读取服务器上的数据,而不是处理响应。 – Quentin 2014-09-02 10:41:10

+0

@FlorianGl - 删除contentType将会很愚蠢。 'data'包含JSON。如果你删除了'contentType',那么你需要重新格式化数据,这样它就形成了编码数据而不是JSON编码数据。 – Quentin 2014-09-02 10:41:48

回答

1

取下AJAX功能 “数据类型: ”两个声明JSON“ 和的contentType: '应用/ JSON的',” 试试这个

function Post() 
{ 
    var table = $('#table4').dataTable(); 
    var data = table.$('input:text').serialize(); 
    console.log(data); 

    $.ajax({ 
     type: "POST", 
     url: '@Url.Action("SaveList")', 
     data: JSON.stringify(data), 

     success: function() { 
      alert('success'); 
     }, 
     error: function() { 
      alert('error'); 
     } 

    }); 
} 
+0

我不是故意投票下来,我似乎无法改变它...很抱歉,我会再试一次 – zzipper72 2015-09-14 15:56:27

0

谢谢你们,我解决的问题。这是我现在的代码,也许以后我会改变它。

的Javascript:

function Post() 
{ 
    var table = $('#table4').dataTable(); 
    var data = table.$('input:text').serialize(); 
    console.log(datas); 
    $.ajax({ 
     type: "POST", 
     url: '@Url.Action("SaveList")', 
     data: data, 
     datatype: 'json', 
     success: function() { 
      alert('success'); 
     } 
    }); 
} 

操作:

public ActionResult SaveList(List<string> inputgrams, List<string> id) 
    { 
     ... 
    }