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?我不知道确切的原因…我不是要投反对票,我似乎无法改变它。。。很抱歉,我稍后再试