如何在javascript客户端获取XMLHttpRequest的响应?

时间:2014-03-15 13:51:28

标签: javascript xmlhttprequest

我需要从Java脚本调用OpenMRS REST API来从OpenMRS获取数据。下面是我的java脚本代码:

    function myfunction(){
    var xhr = new XMLHttpRequest();         

    xhr.open("GET", "http://localhost:8081/openmrs-standalone/ws/rest/v1/person?q=John Smith", true);
    xhr.setRequestHeader("Authorization", "Basic YWRtaW46QWRtaW4xMjM");
    xhr.send(null); 
            alert(xhr.responseXml);
    }

当我使用凭据从浏览器调用给定的URL时,我得到XML响应,如下所示:

<object>
    <results>
        <person>
            <uuid>007037a0-0500-11e3-8ffd-0800200c9a66</uuid>
            <display>John Smith</display>
            <links>
                <link>
                    <rel>self</rel>
                        <uri>
                            NEED-TO-CONFIGURE/ws/rest/v1/person/007037a0-0500-11e3-8ffd-0800200c9a66
                        </uri>
                </link>
            </links>
        </person>
    </results>
</object>

但问题是,javascript警告消息带有值&#34; undefined&#34;。如果我通过Firebug分析网络流量,HTML选项卡会显示200 OK成功消息,但XHR选项卡显示&#34; URL:0请求,大小:0B&#34;信息。在Firefox Web控制台中也可以检查相同的事情,即响应未到来。我发现了一些类似的讨论here,但没有解决方案。如何直接从浏览器调用URL时获取服务器发回的XML响应?

更新代码:

function myfunction(){

        function reqListener () {
        console.log(this.responseXml);
    }
    var xhr = new XMLHttpRequest();     
            xhr.onload = reqListener;

    xhr.addEventListener("progress", updateProgress, false);
    xhr.addEventListener("load", transferComplete, false);
    xhr.addEventListener("error", transferFailed, false);
    xhr.addEventListener("abort", transferCanceled, false); 

    xhr.open("GET", "http://localhost:8081/openmrs-standalone/ws/rest/v1/person?q=John Smith", true);
    xhr.setRequestHeader("Authorization", "Basic YWRtaW46QWRtaW4xMjM");

function updateProgress (oEvent) {
      if (oEvent.lengthComputable) {
        var percentComplete = oEvent.loaded / oEvent.total;
        // ...
      } else {
        // Unable to compute progress information since the total size is unknown
      }
    }

    function transferComplete(evt) {
      alert("The transfer is complete.");
    }

    function transferFailed(evt) {
      alert("An error occurred while transferring the file.");
    }

    function transferCanceled(evt) {
      alert("The transfer has been canceled by the user.");
    }

    xhr.send(null); 
    xhr.onreadystatechange=function()
      {
      if (xhr.readyState==4)
        {
            alert("State: "+xhr.readyState+" and Status: "+xhr.status+" and Response: "+xhr.responseXml);
        }
      }

    }

来自onreadystatechange功能的提醒显示State: 4 and Status: 0 and Response:Undefined。因此,Status不是200,Response未定义。

2 个答案:

答案 0 :(得分:0)

您需要将您的函数(侦听器)分配给属性xhr.onload 您可以在本文Mozilla Developer Network

中阅读有关使用XHR的更多信息

答案 1 :(得分:-1)

在您的代码中执行以下更改,看看是否能解决您的问题:undefined:

xhr.onreadystatechange = function () {
  if (xhr.readyState === 4 && xhr.status === 200) {
        alert("State: " + xhr.readyState + " and Status: " + xhr.status  
              + " and Response: " + xhr.responseXML);
    }
  }
}

与代码中的 xhr.responseXML 相比,请注意 xhr.responseXml