【发布时间】:2018-07-09 10:31:39
【问题描述】:
我已经写了这个 ajax 函数:
$(function () {
$("#showReport").click(function () {
var data = [];
for (var i = 0; i <@Model.LiFilters.Count;i++) {
data[i] = $("#filter" + i).val();
$("#myDiv").html("Hello!");
}
alert('{filters:' + JSON.stringify(data) + '}');
$("#myDiv").remove();
$.ajax({
type: "POST",
url: '@Url.Action("ShowReport", "Report")',
traditional: true,
//data: '{filters:' + JSON.stringify(data) + '}',
data: { filters : data },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(data);
alert(response[0]);
$("#myDiv").html(response[0]);
alert(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
return false;
});
});
并在报表控制器中执行此操作:
[HttpPost]
public JsonResult ShowReport(string[] filters)
{
C_AminReport aminReport = null;
string sErr = string.Empty;
//C_ReportParams aminParams = null;
//C_AminReport aminReport;
string sReport = "Empty Report!";
if (C_AminReport.U_GetReport(aminReport.ID, out aminReport))
{
int iConIndex = Get_ServiceIndex();
aminReport.U_RenderReport(iConIndex, ref sErr);
aminReport.U_GetReportFromAmin(iConIndex, aminReport, out sReport, ref sErr);
}
string[] asReport;
JsonBuilder.U_TryReadJson(sReport, out asReport);
return Json(asReport);
}
但当运行代码时,我看到数据数组已填充但在 ShowReport 操作内部,填充器数组是一个空数组且未填充! 我还在动作参数中尝试了 [FromBody] 标记,但它不起作用! 有什么问题?
【问题讨论】:
-
打开浏览器的开发者工具,看看请求是如何被格式化的。这个 JSON 是什么样子的?
-
@GabrielLuci 看起来像这样:{filters:["1","2","3","4","5","6","7"]}
-
试着把
[FromBody]放在string[] filters前面 -
@GabrielLuci 将其更改为“public JsonResult ShowReport([FromBody]string[] filters)”时,填充器设置为 null insted of string[0]!
标签: jquery asp.net arrays ajax asp.net-core