阿贾克斯,多次调用阿贾克斯

2023-09-11 01:36:36 作者:音调在悬崖-

我不知道它是一个很好的做法,做多 Ajax调用

I wonder is it a good practice to do multiple Ajax calls?

推荐答案

做多AJAX调用是伟大的 - 特别是如果你做他们兼任。有许多来源在那里做到这一点。下面是我使用的:

Doing multiple AJAX calls is great - especially if you do them concurrently. There are a number of sources out there to do that. Here's what I use:

function ajax(url, params, callback)
{
    var xmlhttp;
    var paramstring = "";
    for (postvar in params)
    {
        if (paramstring.length > 0) paramstring += "&";
        paramstring += postvar + "=" + escape(params[postvar]);
    }
    if (window.XMLHttpRequest)
        xmlhttp = new XMLHttpRequest();
    else if (window.ActiveXObject)
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    else
        throw new exception("XMLHTTPRequest failed to initialize.");
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", paramstring.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.onreadystatechange = function ()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) //ok
            callback(unescape(xmlhttp.responseText));
        else if (xmlhttp.readyState==4)
            throw new exception("XMLHTTPRequest loaded with status: " + xmlhttp.status);
    }
    xmlhttp.send(paramstring);
}
 
精彩推荐
图片推荐