gpt4 book ai didi

Javascript HTTP 错误 414。请求 URL 太长

转载 作者:行者123 更新时间:2023-11-28 00:51:24 26 4
gpt4 key购买 nike

Javascript:

function tableToExcel() {

var calendar = document.getElementById("calendar").innerHTML;

window.location.href = "/Calendar/ExportData?calendar=" + calendar;
}

Controller :

 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,为什么?我应该怎么做才能解决这个问题,谢谢?

最佳答案

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

As mentioned by @Brian, the HTTP clients (e.g. browsers) may have their own limits, and HTTP servers will have different limits. Microsoft Support says "Maximum URL length is 2,083 characters in Internet Explorer". IE has problems with URLs longer than that.

您应该为此使用 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] 属性添加到您的 Action 中。

[HttpPost]
public ActionResult ExportData(string calendar)

关于Javascript HTTP 错误 414。请求 URL 太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26739306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com