我试图将ID数组从MVC剃刀视图发送到服务器端(控制器),但是当ID的数量超过2000作为查询字符串长度时,出现以下错误,并且过程停止。
我也尝试使用POST方法发送数据,但出现错误。最后一张屏幕截图是我使用POST方法时遇到的错误。
我在这里粘贴了我的服务器端和Jquery代码。
JQUERY代码
过程1:常规方法
public void ExportPageStatusHistory(string[] PrescriptionIds)
{
ExcelPackage excel = new ExcelPackage();
var fileName = "PageStatusHistoryReport_" + DateTime.Now.ToStr();
var workSheet = excel.Workbook.Worksheets.Add(fileName);
var lst = string.Join(",", PrescriptionIds);
var result = PharmacyReferralService.GetPageASPNHistory(lst);
if (result.Count > 0)
{
workSheet.Cells[1, 1].LoadFromCollection(result, true);
workSheet.Cells.AutoFitColumns();
workSheet.Cells["A1:Z1"].Style.Font.Bold = true;
workSheet.Column(6).Width = 35;
workSheet.Column(7).Width = 35;
using (ExcelRange col = workSheet.Cells[2, 6, 1 + result.Count, 6])
{
col.Style.Numberformat.Format = "yyyy-MM-dd hh:mm AM/PM";
col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
using (ExcelRange col = workSheet.Cells[2, 7, 1 + result.Count, 7])
{
col.Style.Numberformat.Format = "yyyy-MM-dd hh:mm AM/PM";
col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
using (var memoryStream = new MemoryStream())
{
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xlsx");
excel.SaveAs(memoryStream);
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
Jquery Code:
function ExportPrescriptionStatusHistory() {
$('#tbl_PrescriptionsDrugReport tbody tr td:nth-child(1)').each(function () {
items.push($(this).find("a").attr("data-id"));
});
@*var url = '@Url.Action("ExportPageStatusHistory", "Common")?PrescriptionIds=' + items;
location.href = url;*@
var url = '@Url.Action("ExportPageStatusHistory", "Common")';
$.ajax({
url: url,
type: "POST",
data: { PrescriptionIds: items },
async: false,
success: function(response){
},
error:function(xhr, ajaxOptions, thrownError)
{
alert(xhr.responseText);
ShowMessage("Error", "fail");
}
});
}
答案 0 :(得分:0)
您未在path url参数中发送数据。请在请求正文中发送。 所以,只有主意。因为,浏览器的URL长度受限制,最大〜2000个字节。
例如。
服务器代码:
public void updateData([FromBody] string[] data) {
//...
}
客户代码
$.ajax({
type: "POST",
url: "/server",
data: JSON.stringify(data),
contentType: "application/json",
datatype: "html",
success: function (data) {
$('#result').html(data);
}
});
我只有JAVA,我认为C#是相同的。 请查看更多What is the maximum length of a URL in different browsers?