【问题标题】:getJSON not working / phonegap.plist not foundgetJSON 不工作/phonegap.plist 未找到
【发布时间】:2013-01-15 16:32:30
【问题描述】:

我最近下载了 XCode4.5.2 和 PhoneGap2.3.0 和 Cordova 2.3.0 来开发一个 iOS 应用程序。我试图使用这个函数从维基百科 API 中获取一些数据:

<!DOCTYPE html>
   <html>
     <head>

        <script type="text/javascript" src="cordova-2.3.0.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

        <script>
         function get_data(){

                var query = 'Ocean';
                var url = 'http://en.wikipedia.org/w/api.php?callback=?&action=parse&format=json&page='+query+'&prop=text';
                alert(url);

                $.getJSON(url,function(response){
                          alert('Here!');
                          });

                       };


        </script>

        </head>
        <body>
        <input type="button" value="search" onclick="get_data()"   />
        </body>
   </html>

但是,我无法完成这项工作。 alert(url) 工作正常,但 alert('Here!') 不行。 问题似乎出在 PhoneGap 中的 getJSON 函数上。

我在一些帖子中读到,解决方案可能是“将您的域添加到 phonegap.plist 的 ExternalHosts 部分”或“在 config.xml 文件中放置适当的访问标签”。我在这个 2.3.0 版本的 Cordova 中找不到 phonegap.plist 文件(它存在吗?!)。只有 config.xml(我已经确认有 &lt;access origin="*" /&gt;)。

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: javascript xcode api cordova getjson


    【解决方案1】:
    1. 据我所知,getJSON是一个jQuery函数,为什么不包含jQuery的js文件呢?
    2. 你的函数调用者在哪里?如果您希望它在负载时运行,请使用

      $(函数(){ 获取数据(); }

    【讨论】:

    • 我已经编辑了代码。请看上面最后修改的html代码。任何想法为什么警报('Here!')没有出来?谢谢。
    • 尝试对文本使用常规 ajax 函数,可能您的调用响应不包含有效的 JSON 内容.. $.ajax({ url: url, cache: false, dataType:'text', }) .done(function(resp) { alert(resp); });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2012-01-04
    相关资源
    最近更新 更多