3

i am using the open weather map api webservice to make an ajax call inorder to get the current weather using the latitude and longitude the problem is the same call works in my normal php folder but it doesnt work in my phongap app. My ajax call is as shown below

$.ajax({
        type : "GET",
    dataType: "jsonp",
        url  : "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139",
    }).done( function(msg){
     var response = JSON.stringify(msg);
     var parsedResponse = JSON.parse(response);
     alert(parsedResponse.main.temp_min);
    });
});

I have tried without dataType: "jsonp" tried changing it to "json" but nothing works at all. Please help me as I am stuck on this currently.

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
user2801426
  • 83
  • 1
  • 1
  • 6
  • I forgot to add that i am not getting any response ie if i do the following than i get a fail alert in my phone: .done(function(msg){ if(msg){ //do all stringify and parsing } else{ alert("fail"); } }); – user2801426 Dec 21 '13 at 08:41

4 Answers4

1

Have you whitelisted the url in your config.xml?

<access origin="http://api.openweathermap.org" />

Read more: http://docs.phonegap.com/en/3.0.0/guide_appdev_whitelist_index.md.html#Domain%20Whitelist%20Guide

pppontusw
  • 454
  • 3
  • 6
  • Hi...i am new to phone gap and i dont have a config.xml instead i have plugins.xml and cordova.xml in my res/xml folder. So where should i put this ? – user2801426 Dec 23 '13 at 11:21
  • Check out the link, this includes instructions for each platform if you build your project locally: http://docs.phonegap.com/en/3.0.0/guide_appdev_whitelist_index.md.html#Domain%20Whitelist%20Guide – pppontusw Dec 23 '13 at 11:23
  • There is already `` so surely I don't need that? – Jack Dec 18 '17 at 19:01
1

For future searches

yourprojectpath/config.xml

Add or check the following lines

<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />

example screenshot

It may also be required to install "cordova-plugin-whitelist" plugin:

Cordova

cordova plugin add cordova-plugin-whitelist
cordova prepare

Phonegap

phonegap plugin add cordova-plugin-whitelist
phonegap prepare

Also make sure to add the necessary Content-Security-Policy in your file.html:

https://github.com/apache/cordova-plugin-whitelist#content-security-policy

enter image description here

Jzapata
  • 2,442
  • 1
  • 12
  • 9
0
var weather = ""
        var ajax_call = "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139";
        $.ajax({
           type: "GET",
           url: ajax_call,
           dataType: "jsonp",
           success: function(response){
                $.each(response, function(key, value) {
                   //alert(key+"====="+value)
                    if(key == "coord"){
                        weather += '<div><strong>coord<strong><div>';
                        $.each(value, function(key, value) {
                            if(key == "lon")
                               weather += '<div>lon: '+value+'<div>';
                           if(key == "lat")
                               weather += '<div>lat: '+value+'<div>';
                        });
                    }
                    if(key == "weather"){
                        weather += '<div><strong>weather<strong><div>';
                        $.each(value, function(key, value) {

                           if(value.id)
                               weather += '<div>id: '+value.id+'<div>';
                           if(value.main)
                               weather += '<div>main: '+value.main+'<div>';
                           if(value.description)
                               weather += '<div>description: '+value.description+'<div>';                             

                        });                           
                    }
                    if(key == "main"){
                        weather += '<div><strong>main<strong><div>';
                        $.each(value, function(key, value) {
                            if(key == "temp")
                               weather += '<div>temp: '+value+'<div>';
                           if(key == "temp_min")
                               weather += '<div>temp_min: '+value+'<div>';
                           if(key == "temp_max")
                               weather += '<div>temp_max: '+value+'<div>';
                           if(key == "pressure")
                               weather += '<div>pressure: '+value+'<div>';
                           if(key == "sea_level")
                               weather += '<div>sea_level: '+value+'<div>';
                           if(key == "grnd_level")
                               weather += '<div>grnd_level: '+value+'<div>';
                           if(key == "humidity")
                               weather += '<div>humidity: '+value+'<div>';

                        });                           
                    }

                });
                alert(weather) 
                console.log(weather)

           }
        }).done(function() {

        })  
Ved
  • 2,701
  • 2
  • 22
  • 30
0

it's a problem of white-list https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/ Install it and go to config.xml to allow