我想用jquery Ajax提交表单。但它没有显示任何消息。我已经按了this个问题和this个问题。但我没有得到我的解决方案。我的jquery代码是:
$(document).ready(function () {
$("#btnSave").submit(function (e) {
e.preventDefault();
var myTableArray = [];
$("table#vouchTable tr").not(':first').each(function () {
var arrayOfThisRow = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
tableData.each(function () { arrayOfThisRow.push($(this).text()); });
myTableArray.push(arrayOfThisRow);
}
}); ///This Part works fine
///But this part does not work
$.ajax({
url: '/Accounts/VoucherEntry',
type: 'POST',
data: JSON.stringify(myTableArray),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert(msg);
},
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
return false;
////////////////////
});
});
答案 0 :(得分:0)
试试这个:
缺少$(document).ready(function () {
缺少$("#btnSave").submit(function (e) {
$(document).ready(function () {
$("#btnSave").submit(function (e) {
e.preventDefault();
var myTableArray = [];
$("table#vouchTable tr").not(':first').each(function () {
var arrayOfThisRow = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
tableData.each(function () { arrayOfThisRow.push($(this).text()); });
myTableArray.push(arrayOfThisRow);
}
});
var data = JSON.stringify(myTableArray);
$.ajax({
url: '/Accounts/VoucherEntry',
type: 'POST',
data: data,
dataType: 'json',
success: function (msg) {
alert(msg);
},
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
}); // <----- Missing brackets!!!
}); // <----- Missing brackets!!!
答案 1 :(得分:0)
将数据格式化为:
data: {data:myTableArray},
c#中的按键data