无法返回xmlhttp.responseText? [英] Can't return xmlhttp.responseText?

查看:124
本文介绍了无法返回xmlhttp.responseText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此问题的任何见解?运行时,代码不会产生任何效果。页面上不显示任何文字。如果我取消注释注释行,则会显示xml结果。为什么我不能把它作为变量传递? (我确实得到了警告,fyi,所以正在调用该函数。)

Any insight into the problem here? When run, the code yields nothing. No text appears on the page. If I uncomment the commented line, the xml results appear. Why can't I pass it as a variable? (I do get the alert, fyi, so the function is being called.)

 <script type="text/javascript">
           function loadXMLDoc(parameterString)
               {
                   alert("loadXMLDoc has been called.");
                   var xmlhttp = new XMLHttpRequest();

                   xmlhttp.onreadystatechange=function()
                   {
                       if (xmlhttp.readyState==4 && xmlhttp.status==200)
                       {

                  //document.getElementById("xmlResults").innerHTML = xmlhttp.responseText;
                               alert("Got the response!");
                               return xmlhttp.responseText;
                           }
                           else document.getElementById("xmlResults").innerHTML = "No results."
                       }

                       var url =  "http://metpetdb.rpi.edu/metpetwebsearchIPhone.svc?" + parameterString;
                   xmlhttp.open("GET",url,true);
                   xmlhttp.send();
               }
        </script>



        <script type="text/javascript">

       $(function(){

        //left out irrelevant code which creates the var "parameters"

         var results = loadXMLDoc(parameters);

         document.getElementById("xmlresults").innerHTML = results;

       });


       </script>


<body>
<div id="xmlResults"></div>
</body>


推荐答案

根据定义,异步调用执行实际工作让调用者等待结果。您确实需要使用回调函数,例如:

By definition, an asynchronous call performs the real work without making the caller wait for the result. You do need to use a callback function, for example:

<script type="text/javascript">
  function loadXMLDoc(parameterString, onComplete, onError) {
    alert("loadXMLDoc has been called.");
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
        if(xmlhttp.status==200) {
          //document.getElementById("xmlResults").innerHTML = xmlhttp.responseText;
          alert("Got the response!");
          onComplete(xmlhttp.responseText);
        } else {
          onError();
        }
      }
    };

    var url =  "http://metpetdb.rpi.edu/metpetwebsearchIPhone.svc?" + parameterString;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
  }
</script>

<script type="text/javascript">
  $(function(){
    //left out irrelevant code which creates the var "parameters"
    loadXMLDoc(parameters, function(results) {
      // this function will be called if the xmlhttprequest received a result
      document.getElementById("xmlresults").innerHTML = results;
    }, function() {
      // this function will be called if xhr failed
      document.getElementById("xmlResults").innerHTML = "No results.";
    });
  });
</script>

顺便说一下,由于你已经在使用jQuery,你应该只使用其内置的AJAX功能,而不是构建自定义的xmlhttprequest。

By the way, since you are already using jQuery, you should just use its builtin AJAX functionality instead of building your custom xmlhttprequest.

这篇关于无法返回xmlhttp.responseText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆