【问题标题】:Get simpleXMLElement with jQuery使用 jQuery 获取 simpleXMLElement
【发布时间】:2010-02-19 20:46:52
【问题描述】:

如何使用 jquery 从 php 文件中检索 simpleXMLElement 变量的内容?

【问题讨论】:

    标签: php jquery xml simplexml


    【解决方案1】:

    发送SimpleXMLElement::asXML()的返回值

    独立示例:

    <?php
    if ( isset($_GET['foo']) ) {
      $foo = new SimpleXMLElement('<foo />');
      $foo->bar->baz[] = date('Y-m-d H:i:s');
      $foo->bar->baz[] = rand(1,100)." la la la";
      header('Content-type: application/xml; charset=utf-8');
      echo $foo->asXML();
      die;
    }
    ?><html>
      <head>
        <title>...</title>
        <script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script>
        <script type="text/javascript">
          function foo() {
            $.ajax({
              type: "GET",
              url: "?",
              data: { "foo":1 },
              dataType: "xml",
              success: function(xml) {
                 $(xml).find("baz").each(function(){
                   $("#div1").append($(this));
                   $("#div1").append("<br />");
                 })
              },
              error: function() {
                alert("that didn't work");
              }
            });
          }
        </script>
      </head>
      <body>
        <button onclick="foo()">click</button>
        <div id="div1">...</div>
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 2020-09-21
      • 2018-02-13
      • 1970-01-01
      • 2011-05-07
      • 2013-02-24
      • 2017-03-30
      • 1970-01-01
      • 2012-12-30
      相关资源
      最近更新 更多