Cannot get JSON results using PhoneGap and jQuery on iPhone App

非 Y 不嫁゛ 提交于 2019-12-23 01:27:14

问题


Currently I use this code:

<script type="text/javascript">

   $(document).ready(function() {
    var url =  "http://openexchangerates.org/latest.json";
    $.getJSON(url + "?callback=?", null,function(data) {

       var currencies = [ "USD", "EUR", "JPY", "GBP", "CHF", "AUD", "CAD", "EUR", "SEK", "HKD", "NOK", "NZD", "MXN", "SGD", "KRW", "RON", "BGN", "RUB", "PLN", "DKK" ];
       var myElementToAppendTo = $("#content");

       $.each(data.rates, function(key, value) {
         value2 = 1 / value;
         valueForEuro = value;  
         value = accounting.formatMoney(value, "", 4, ",", "."); 
         value2 = accounting.formatMoney(value2, "$", 4, ",", ".");

         euro = data.rates.EUR;
         value3 = valueForEuro / euro;
         value4 = 1 / value3;
         value3 = accounting.formatMoney(value3, "", 4, ",", "."); 
         value4 = accounting.formatMoney(value4, "&euro;", 4, ",", ".");

            if(jQuery.inArray(key,currencies) > -1) {
                myElementToAppendTo.append('<div class="currencyBox"><div class="currency">'+key+'</div><div class="tab1"><div class="half">'+value+'</div><div class="half">'+value3+'</div></div><div class="tab2"><div class="half">1 '+key+' = '+value2+' </div><div class="half">1 '+key+' = '+value4+' </div></div></div>');
            }
       });

    });
    });


</script>

It works on any browser local/server but when I build an iPhone app the JSON results do not display. Any ideas why?


回答1:


You need to whitelist the server you are connecting to (this can be a wildcard as well). This is done in your PhoneGap.plist file. From the Phonegap docs:

Also, the latest code has the new white-list feature. If you are referencing external hosts, you will have to add the host in PhoneGap.plist under the "ExternalHosts" key. Wildcards are ok. So if you are connecting to "http://phonegap.com", you have to add "phonegap.com" to the list (or use the wildcard "*.phonegap.com" which will match subdomains as well).




回答2:


Not sure if this will help but I ended up using a google feeds plugin because it was much easier when I had this same problem. Had to jump through a lot of hoops to do it w/o.

Link: http://jquery.malsup.com/gfeed/

Otherwise you need to modify your plist...

Phonegap reads a setting called 'ExternalHosts' to check what can be allowed.

Open phonegap.plist - the key should in there and you should add a new one for your domain.



来源:https://stackoverflow.com/questions/8494911/cannot-get-json-results-using-phonegap-and-jquery-on-iphone-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!