开发者

How to overcome Ajax response size limit?

开发者 https://www.devze.com 2023-01-17 11:26 出处:网络
Here I had use ajax, in which when ajax give response at that time it cut the text automatically after some characters (6140 characters), so is it limit of ajax response? If so, then how can I solve t

Here I had use ajax, in which when ajax give response at that time it cut the text automatically after some characters (6140 characters), so is it limit of ajax response? If so, then how can I solve this?

If the string I am passing to JavaScript from jsp is too la开发者_Go百科rge, JavaScript does not get it all the data. The magic number appears to be 6140 characters, anything below that it works fine, anything above and if I alert the ajax.response, I see the string is cutoff. Any ideas where this limitation is defined?

I had use pure ajax and learned from www.w3school.com, so just same like that I had use ajax and get response in XML tag.

My code :

var XmlHttpRd = false;
function CreateXmlHttpRd()
{
    try
    {
        XmlHttpRd = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            XmlHttpRd = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch(oc)
        {
            XmlHttpRd = new XMLHttpRequest();//for browser mozila, opera, firefox.
        }
    }
}

function getAllChat()
{   
    var requestUrl = "remotePage.jsp?r="+Math.random();
    //alert(requestUrl);
    CreateXmlHttpRd();
    if(XmlHttpRd)
    {   
        XmlHttpRd.onreadystatechange = HandleResponse;
        XmlHttpRd.open('GET', requestUrl,  true);
        XmlHttpRd.send(null);       
    }
}

function RefreshStatus()
{
    if(refStatus)
        getAllChat();
}

function HandleResponse()
{
    //alert(XmlHttpRd.readyState);
    //alert(XmlHttpRd.status);
    if(XmlHttpRd.readyState == 4 && XmlHttpRd.status == 200)
    {
        //alert(XmlHttpRd.responseText); // here i uncomment alert then respnse text seen cutofff
        var XmlRoot = XmlHttpRd.responseXML.documentElement;

                //// Some Code here to get data//////////
        }
}


Are you sure the response is cut off and that it is not just the browser that does not show all text in the alert dialog? I have some Ajax requests that return very long exception traces that are displayed in an alert dialog and they are usually cut off by the browser somewhere. It is probably done to prevent a page from opening a alert dialog that would be too big for the screen.

If you want to get the full text you could add a <textarea id="debug"></textarea> to your page and use document.getElementById('debug').value = XmlHttpRd.responseText to get the full response.

I would recommend that you don't use a global XMLHttpRequest object because it could be overwritten when you start a second request. Then the onreadystatechange handlers of both requests could be executed and process the second request twice. You could do it like this:

function doRequest() {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            if (request.status == 200) {
                // process response
            }
        }
    }
    request.open(...);
    request.send(...);
}

In this code every handler will access the request object that was defined in the function when the request was started and can not process a wrong request.

0

精彩评论

暂无评论...
验证码 换一张
取 消