使用ajax

时间:2017-01-27 16:51:29

标签: jquery ajax xml

我正在尝试从API(实际上是BBB)返回信息,其响应确实如下:

<?xml version="1.0"?>
<response>
    <returncode>SUCCESS</returncode>
    <meetingID>ldapreader</meetingID>
    <attendeePW>accounts123</attendeePW>
    <moderatorPW>accounts456</moderatorPW>
    <createTime>1485524358342</createTime>
    <voiceBridge>24686</voiceBridge>
    <dialNumber>613-555-1234</dialNumber>
    <createDate>Fri Jan 27 14:39:18 CET 2017</createDate>
    <hasUserJoined>false</hasUserJoined>
    <duration>0</duration>
    <hasBeenForciblyEnded>false</hasBeenForciblyEnded>
    <messageKey>duplicateWarning</messageKey>
    <message>
        This conference was already in existence and may currently be in progress.
    </message>
</response>

正如Chrome所说,这个XML文件似乎没有与之关联的任何样式信息...

我已将此作为脚本基于我在本网站上发现的一些帖子,但我被卡住了......

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>

    function myCall() {
        var request = $.ajax({
            url: "<?php echo $fulllink; ?>",
            type: "GET",            
            dataType: "xml"
        });

        request.done(function(xml) {
                  // Parse the xml file and get data
        var xmlDoc = $.parseXML(xml),
            $xml = $(xmlDoc);
        $xml.find('response').each(function () {
            $("#mybox").append($(this).text() + "<br />");
        });
          //  $("#mybox").html(xml);          
        });

        request.fail(function(jqXHR, textStatus) {
            alert( "Request failed: " + textStatus );
        });
    }
</script>
      <style type="text/css">
            #mybox {
                width: 300px;
                height: 250px;
                border: 1px solid #999;
            }
        </style>
 <input type="button" value="Update" onclick="myCall()" /> 
     <div id="mybox">  </div>

无论我尝试找到什么(响应或返回代码,消息......),我的div仍为空,我在Chrome的调试工具中看不到任何错误。我可以看到XHR请求很好......

也许知识渊博的人可以帮助我吗? 谢谢你的灯! 塞德里克

2 个答案:

答案 0 :(得分:1)

您可以使用jQuery.parseXML()来完成此操作,

  

...使用浏览器的本机解析功能来创建有效的XML文档。然后可以将此文档传递给jQuery以创建可以遍历和操作的典型jQuery对象。

var xml="<?xml version="1.0"?>
<response>
    <returncode>SUCCESS</returncode>
    <meetingID>ldapreader</meetingID>
    <attendeePW>accounts123</attendeePW>
    <moderatorPW>accounts456</moderatorPW>
    <createTime>1485524358342</createTime>
    <voiceBridge>24686</voiceBridge>
    <dialNumber>613-555-1234</dialNumber>
    <createDate>Fri Jan 27 14:39:18 CET 2017</createDate>
    <hasUserJoined>false</hasUserJoined>
    <duration>0</duration>
    <hasBeenForciblyEnded>false</hasBeenForciblyEnded>
    <messageKey>duplicateWarning</messageKey>
    <message>
        This conference was already in existence and may currently be in progress.
    </message>
</response>";  
  xmlDoc = $.parseXML( xml ),
      $xml = $( xmlDoc ),
      $meetingID = $xml.find( "meetingID" );


$( "#anotherElement" ).append( $meetingID.text() );

答案 1 :(得分:-1)

使用simpleXML,就像阅读XML然后通过易于阅读的对象访问它的内容一样简单。

<?php

$xml="<?xml version='1.0'?>
<response>
   <returncode>SUCCESS</returncode>
   <meetingID>ldapreader</meetingID>
   <attendeePW>accounts123</attendeePW>
   <moderatorPW>accounts456</moderatorPW>
   <createTime>1485524358342</createTime>
   <voiceBridge>24686</voiceBridge>
   <dialNumber>613-555-1234</dialNumber>
   <createDate>Fri Jan 27 14:39:18 CET 2017</createDate>
   <hasUserJoined>false</hasUserJoined>
   <duration>0</duration>
   <hasBeenForciblyEnded>false</hasBeenForciblyEnded>
   <messageKey>duplicateWarning</messageKey>
   <message>
       This conference was already in existence and may currently be in progress.
   </message>
</response>";


$xmlString=simplexml_load_string($xml);
echo $xmlString->returncode[0];


?>