首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将CallbackFunction作为Ajax-Request的参数传递

将CallbackFunction作为Ajax-Request的参数传递
EN

Stack Overflow用户
提问于 2013-04-03 05:35:57
回答 3查看 148关注 0票数 0

我想为我的网站异步获取内容。现在我的服务器有多个请求,我想将“连接内容”分离到另一个函数中会很棒:

代码语言:javascript
复制
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“函数,如下所示:

代码语言:javascript
复制
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:

代码语言:javascript
复制
...
xmlHttp.onreadystatechange = function()
        {
            if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
            {
            document.getElementById("searchResult").innerHTML = xmlHttp.responseText;
            document.getElementById("searchResult").style.border="1px solid #A5ACB2";
            }
      }

..。它完美地工作了:,非常明确的是,错误是关于以错误的方式传递函数,我不知道。因为我的情况是我提出了一个自己的问题。

感谢您的回复:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-04-03 05:38:48

您正在尝试从回调中使用xmlHttp,但它超出了范围。我建议采取以下方法:

代码语言:javascript
复制
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";
  });
}
票数 2
EN

Stack Overflow用户

发布于 2013-04-03 05:50:40

在我的XMLHttp包装器实现中,我使用Function.prototype.call从XMLHttp对象作用域调用回调函数,因此您可以使用关键字this来访问属性,为方便起见,我还将responseText作为参数传递。

代码语言:javascript
复制
if(typeof func=="function")
    func.call(xmlHttp, xmlHttp.responseText);

您可以轻松地在回调上打印响应

代码语言:javascript
复制
function callback(response){
    alert(response);
}

但是您仍然可以访问XMLHttp对象的所有属性

代码语言:javascript
复制
function callback(response){
    alert(this.status); //200
    alert(response);
}

完整代码:

代码语言:javascript
复制
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";
  });
}
票数 1
EN

Stack Overflow用户

发布于 2013-04-03 06:16:53

由于您传递给xmlHttp.onreadystatechange的函数是以xmlHttp作为上下文调用的,因此您可以使用this来引用该对象。

代码语言:javascript
复制
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";
    }
  });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15774865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档