【问题标题】:xml2json jQuery plugin not workingxml2json jQuery插件不起作用
【发布时间】:2015-09-28 18:12:35
【问题描述】:

我正在使用jQuery XML to JSON Plugin by Fyneworks.com

为什么这段代码不起作用?

index.html

<!DOCTYPE html>
<html>
<head>
<script src="build/jquery.xml2json.js"></script>
<script src="build/jquery.js"></script>
<script>
$(document).ready(function() {
    $('button').click(function() {
        $.get('menu.xml', function(xml){
            var json = $.xml2json(xml);
            alert(json.message);
        });
    });
});
</script>
</head>
<body>
<button>Menu List</button>
</body>
</html>

menu.xml

<xml>
 <message>Hello world</message>
</xml>

错误是:Uncaught TypeError: $.xml2json is not a function

【问题讨论】:

    标签: javascript java jquery json xml


    【解决方案1】:

    jquery.xml2json.js 依赖于jquery。所以我们必须先加载jquery.js,然后再加载jquery.xml2json.js

    <script src="build/jquery.js"></script>
    <script src="build/jquery.xml2json.js"></script>
    

    【讨论】:

      【解决方案2】:

      如果您要在文档准备好后设置事件处理程序,则需要使用 on()

      $(document).ready(function(){
          $("button").on("click", function(){
              $.get('menu.xml', function(xml){
                  var json = $.xml2json(xml);
                  alert(json.message);
              });
          });
      });
      

      否则,您需要将这段代码放在在 html 中创建元素之后,就像这样。

      <!DOCTYPE html>
      <html>
      <head>
      </head>
      <body>
      <button>Menu List</button>
      
      <script src="build/jquery.xml2json.js"></script>
      <script src="build/jquery.js"></script>
      <script>
          $("button").click(function(){
              $.get('menu.xml', function(xml){
                 var json = $.xml2json(xml);
                 alert(json.message);
              });
          });
      </script>
      </body>
      </html>
      

      【讨论】:

      • 未捕获的类型错误:$.xml2json 不是函数
      猜你喜欢
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 2011-02-13
      • 2014-06-24
      • 2011-08-30
      相关资源
      最近更新 更多