发送大数据作为查询字符串,出现错误“ HTTP错误414。请求URL太长”

时间:2018-09-26 08:39:48

标签: jquery asp.net asp.net-mvc asp.net-ajax

我试图将ID数组从MVC剃刀视图发送到服务器端(控制器),但是当ID的数量超过2000作为查询字符串长度时,出现以下错误,并且过程停止。

我也尝试使用POST方法发送数据,但出现错误。最后一张屏幕截图是我使用POST方法时遇到的错误。

enter image description here

我在这里粘贴了我的服务器端和Jquery代码。

服务器端代码 enter image description here

JQUERY代码

过程1:常规方法

enter image description here

过程2:使用POST方法处理sendig数据的替代方法 enter image description here

enter image description here

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

1 个答案:

答案 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?