What is max size of ajax response data?

2019-05-23 22:57发布

问题:

I send request from asp.net page and then wait for response, calling GetCommand via SetInterval method:

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

In AjaxFinishedGet(result, status) I try to extract my data:

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

However, if Text size is more than 102330 bytes - instead of AjaxFinishedGet, AjaxFailedGet is called :(

I have not found any info neither about some limitation of ajax response data size nor about javascript variable size, at least such variable can hold 1MB without problems. Actually Text may contain 1MB of data...

Where is the problem?

回答1:

Well, multiple requests can cause error, perhaps a good solution is to verify that there is no active current request.

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

}

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


回答2:

James Black, i have no any info about the error:

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

WebServer is IIS7 on Vista x32

zazk, thanks, putting such check is a good point and i will use this flag for reliability. However, in the given case, i don't think it could be the reason of the problem: response output does work always, when data size is 102330 and doesn't work starting from 102331 (i am using new String('x', 102330), so it cannot be any special character problem or something like that).



回答3:

There is a default setting in the web.config file for .NET is 4mb

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

Because maxRequestLength is an INT, the theoretical maximum is the INT max value which is 2,147,483,647



回答4:

I just had a nasty error which i tracked down to the length of a string being returned from an AJAX call. It was about 63639 (iirc) which is close to the limit of a ushort (again, iirc!) (I guess iis appends its own stuff to make up the rest of the character limit).

I managed to strip the all the styling out of the html in the string and append it through JQuery when it was received by the client! :)