我想为我的网站异步获取内容。现在我的服务器有多个请求,我想将“连接内容”分离到另一个函数中会很棒:
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(“Classs...”,"sVal..",function(){...}“)中给出的函数直接分配给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";
}
}..。它完美地工作了:,非常明确的是,错误是关于以错误的方式传递函数,我不知道。因为我的情况是我提出了一个自己的问题。
感谢您的回复:)
发布于 2013-04-03 05:38:48
您正在尝试从回调中使用xmlHttp,但它超出了范围。我建议采取以下方法:
function conToServerAsync( url, postParams, func )
{
var xmlHttp;
if( window.XMLHttpRequest )
{
xmlHttp = new XMLHttpRequest();
}
else
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
func.call(xmlHttp, xmlHttp);
}
}
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(postParams);
}
function findSearchResult(value)
{
conToServerAsync( "Classes/Async/liveSearch.php", "sVal="+value, function(xhr)
{
document.getElementById("searchResult").innerHTML = xhr.responseText;
document.getElementById("searchResult").style.border="1px solid #A5ACB2";
});
}发布于 2013-04-03 05:50:40
在我的XMLHttp包装器实现中,我使用Function.prototype.call从XMLHttp对象作用域调用回调函数,因此您可以使用关键字this来访问属性,为方便起见,我还将responseText作为参数传递。
if(typeof func=="function")
func.call(xmlHttp, xmlHttp.responseText);您可以轻松地在回调上打印响应
function callback(response){
alert(response);
}但是您仍然可以访问XMLHttp对象的所有属性
function callback(response){
alert(this.status); //200
alert(response);
}完整代码:
function conToServerAsync( url, postParams, func ){
var xmlHttp;
if( window.XMLHttpRequest ){
xmlHttp = new XMLHttpRequest();
}
else{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState==4 && xmlHttp.status==200){
if(typeof func=="function")
func.call(xmlHttp, xmlHttp.responseText);
}
}
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(postParams);
}
function findSearchResult(value){
conToServerAsync( "Classes/Async/liveSearch.php", "sVal="+value, function(response){
document.getElementById("searchResult").innerHTML = response;
document.getElementById("searchResult").style.border="1px solid #A5ACB2";
});
}发布于 2013-04-03 06:16:53
由于您传递给xmlHttp.onreadystatechange的函数是以xmlHttp作为上下文调用的,因此您可以使用this来引用该对象。
function findSearchResult(value)
{
conToServerAsync( "Classes/Async/liveSearch.php", "sVal="+value, function()
{
if(this.readyState == 4 && this.status == 200)
{
document.getElementById("searchResult").innerHTML = this.responseText;
document.getElementById("searchResult").style.border="1px solid #A5ACB2";
}
});
}https://stackoverflow.com/questions/15774865
复制相似问题