【问题标题】:Javascript HTTP Error 414. The request URL is too longJavascript HTTP 错误 414。请求 URL 太长
【发布时间】:2014-11-04 15:47:49
【问题描述】:

Javascript:

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

如果我使用 window.location.href 将 document.getElementById("calendar").innerHTML 发送到 ExportData,我会看到下面的 url 异常

Request URL Too Long

HTTP Error 414. The request URL is too long.

我发送参数但浏览器获取参数作为 url 为什么?我应该怎么做才能解决这个问题谢谢?

【问题讨论】:

    标签: javascript asp.net-mvc url


    【解决方案1】:

    最大网址大小为 2,083 个字符。

    正如@Brian 所提到的,HTTP 客户端(例如浏览器)可能有 他们自己的限制,HTTP服务器会有不同的限制。 Microsoft 支持说“最大 URL 长度为 2,083 个字符 Internet Explorer”。IE 的 URL 长度超过这个值。

    您应该为此使用 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多