Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 操作在ajax post之后接收空字符串_Jquery_Asp.net Mvc_Json - Fatal编程技术网

Jquery 操作在ajax post之后接收空字符串

Jquery 操作在ajax post之后接收空字符串,jquery,asp.net-mvc,json,Jquery,Asp.net Mvc,Json,我有一个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.

我有一个
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");
    }
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)
    {
       ...
    }
行动:

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");
    }
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)
    {
       ...
    }

问题是操作接收到一个空的sting
console。日志(数据)
显示有内容,如果我在控制器上放置断点,它将停止,但serializedString为空。哪里会有问题?谢谢

从ajax函数“dataType:“json”和contentType:“application/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');
        }

    });
}

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

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");
    }
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)
    {
       ...
    }
行动:

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");
    }
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)
    {
       ...
    }
public ActionResult存储列表(列表输入程序,列表id)
{
...
}

尝试
dataType:'text'
并删除
contentType
@FlorianGl-说
dataType:'text'
没有帮助。问题是读取服务器上的数据,而不是处理响应。@FlorianGl-删除
contentType
将是愚蠢的。
数据
包含JSON。如果删除
contentType
,则需要重新格式化数据,使其成为表单编码的数据,而不是JSON编码的数据。@user2710900-为什么要对数据进行表单编码(使用
serialize()
),然后将结果字符串转换为JSON?我不知道确切的原因…我不是要投反对票,我似乎无法改变它。。。很抱歉,我稍后再试