Andrei RRR
Andrei RRR

Reputation: 3162

Cannot get JSON results using PhoneGap and jQuery on iPhone App

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?

Upvotes: 1

Views: 2649

Answers (2)

Daniel Kurka
Daniel Kurka

Reputation: 7985

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).

Upvotes: 4

sirmdawg
sirmdawg

Reputation: 2579

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.

Upvotes: 0

Related Questions