我想异步获取我网站的内容。现在,我有我的服务器的多个请求我认为这将是巨大的“conntection东西”单独成另一种功能:将CallbackFunction作为参数传递给Ajax请求
function conToServerAsync(url, postParams, func)
{
var xmlHttp;
if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
else
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange = func;
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(postParams);
}
现在我正在执行像“conToServerAsync”功能,这个其他功能这样的:
function findSearchResult(value)
{
conToServerAsync("Classes/Async/liveSearch.php", "sVal="+value, function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
document.getElementById("searchResult").innerHTML = xmlHttp.responseText;
document.getElementById("searchResult").style.border="1px solid #A5ACB2";
}
});
}
现在居然什么有趣的关于我的情况是,我检查了一切,这是在传递每一个参数是有效的。然后我试图来分配在最后一个参数给出 “conToServerAsync( ”类的功能......“, ”SVAL ..“,函数(){...}” 直接到的onreadystatechange:
...
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
document.getElementById("searchResult").innerHTML = xmlHttp.responseText;
document.getElementById("searchResult").style.border="1px solid #A5ACB2";
}
}
......并且它完美地工作:S所以最初的错误是关于通过错误的方式传递函数,我不知道,因为我的情况是,我特别提出了自己的问题。回答:)
或只使用'this' – Musa
作品完美:)感谢名单,让我晚上:) – Hans123
@Musa尼斯,改变了代码,允许这两种风格(当然不使用'this'的地方,我可以用它)。 – bfavaretto