【问题标题】:What is max size of ajax response data?ajax 响应数据的最大大小是多少?
【发布时间】:2010-12-15 03:44:23
【问题描述】:

我从asp.net页面发送请求,然后等待响应,通过SetInterval方法调用GetCommand:

    function GetCommand(id, sid) {
    getCommandResponse = $.ajax({
        type: "POST",
        async: true,
        url: "../WebServices/TSMConsole.asmx/GetCommand",
        data: "{'param' : '" + id + "', 'sid' : '" + sid + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result, status) {
            AjaxFinishedGet(result, status);
            getCommandResponse = null;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            AjaxFailedGet(XMLHttpRequest, textStatus, errorThrown);
            getCommandResponse = null;
        }
    });
}

在 AjaxFinishedGet(result, status) 我尝试提取我的数据:

    function AjaxFinishedGet(xml, status) {
    endDate = new Date();
    if (xml.d.IsPending == 'false' || endDate - startDate > timeoutMSec) {
        if (getCommand != null) {
            window.clearInterval(getCommand);
            getCommand = null;
            WriteGetCommand(xml.d.Text);
            $("#showajax").fadeOut("fast");
        }
    }
}

但是,如果文本大小超过 102330 字节 - 而不是 AjaxFinishedGet,则调用 AjaxFailedGet :(

我没有找到任何关于 ajax 响应数据大小的限制或关于 javascript 变量大小的信息,至少这样的变量可以容纳 1MB 没有问题。实际上 Text 可能包含 1MB 的数据...

问题出在哪里?

【问题讨论】:

  • 抛出什么错误?您使用的是哪个网络服务器?

标签: ajax size response max


【解决方案1】:

好吧,多个请求可能会导致错误,也许一个好的解决方案是验证当前没有活动的请求。

var loading = false;
function GetCommand(id, sid) {
    if (loading) {return false;}
    loading =  true;
        getCommandResponse = $.ajax({
         ....
         ....
        });

}

function AjaxFinishedGet(xml, status) {
    loading = false;
    ...
    ...
}

【讨论】:

    【解决方案2】:

    James Black,我没有任何关于该错误的信息:

    function AjaxFailedGet(XMLHttpRequest, textStatus, errorThrown) {
        $("#<%= tbResponse.ClientID %>").text(errorThrown);
        if (getCommand != null) {
            window.clearInterval(getCommand);
            $("#showajax").fadeOut("fast");
        }
    }
    
    • errorThrown 为空。

    Vista x32 上的 WebServer 是 IIS7

    扎克, 谢谢,进行这样的检查是一个很好的观点,我将使用这个标志来保证可靠性。但是,在给定的情况下,我不认为这可能是问题的原因:当数据大小为 102330 并且从 102331 开始时(我正在使用 new String('x' , 102330),所以它不可能是任何特殊字符问题或类似问题)。

    【讨论】:

      【解决方案3】:

      .NET 的 web.config 文件中有一个默认设置是 4mb

      <system.web>
      <httpRuntime maxRequestLength="4096" />
      </system.web>
      

      因为maxRequestLength是一个INT,所以理论上的最大值是INT最大值,即2,147,483,647

      【讨论】:

        【解决方案4】:

        我刚刚遇到了一个令人讨厌的错误,我追踪到了从 AJAX 调用返回的字符串的长度。大约是 63639 (iirc),接近 ushort 的限制(再次,iirc!)(我猜 iis 会附加自己的东西来弥补字符限制的其余部分)。

        我设法从字符串中的 html 中去除所有样式,并在客户端收到它时通过 JQuery 附加它! :)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-16
          • 1970-01-01
          • 2013-01-09
          • 1970-01-01
          • 2013-02-26
          • 2012-06-30
          • 2011-03-13
          相关资源
          最近更新 更多