的Javascript HTTP错误414请求URL太长
问题描述:
的Javascript:的Javascript HTTP错误414请求URL太长
function tableToExcel() {
var calendar = document.getElementById("calendar").innerHTML;
window.location.href = "/Calendar/ExportData?calendar=" + calendar;
}
控制器:
public ActionResult ExportData(string calendar)
{
string headerTable = calendar;
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + "Calendar" + ".xls;");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
Response.Write(headerTable);
Response.Flush();
Response.End();
return new EmptyResult();
}
如果我发送的document.getElementById( “日历”)innerHTML来通过使用window.location.href导出数据我看到下面的url异常
Request URL Too Long
HTTP Error 414. The request URL is too long.
我发送参数,但浏览器获取参数为url为什么?我应该怎么做才能解决这个问题,谢谢?
答
最大网址大小为2,083个字符。
正如@布莱恩所说,HTTP客户端(例如浏览器)可能有自己的限制,并且HTTP服务器会有不同的限制。 Microsoft支持表示“最大URL长度是 Internet Explorer中的2,083个字符”。 IE浏览器遇到问题的时间比这个长。
你应该使用POST方法对于这一点,所以:
这段代码创建一个表格,并追加到身体并提交。
function tableToExcel() {
var calendar = document.getElementById("calendar").innerHTML;
var form = $('<form action="ExportData" method="post"><input type="text" name="calendar" /></form>')
form.find('input').val(calendar);
form.appendTo('body').submit();
}
我建议您将[HttpPost]
属性添加到您的操作中。
[HttpPost]
public ActionResult ExportData(string calendar)