Ajax, multiple calls
I开发者_如何学Go wonder is it a good practice to do multiple Ajax calls
?
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);
}
精彩评论