2013-12-21 47 views
2

我使用打開的天氣圖api webservice使ajax調用以獲取當前天氣使用經緯度問題是相同的調用工作在我的正常php文件夾中但它不適用於我的phongap應用程序。我的Ajax調用如下圖所示AJAX調用不能在Phonegap中工作,但工作正常

$.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); 
    }); 
}); 

我已經試過沒有dataType: "jsonp"試圖將其更改爲"json"但沒有在所有工作。請幫助我,因爲我目前卡住了。

+0

我忘了補充一點,我沒有得到,如果我做下面的比我得到我的手機故障警報任何迴應,即: .done(函數(MSG){ 如果(MSG){// 做的所有字符串化和解析 } 其他{ 警報( 「失敗」);} }); – user2801426

回答

1

您是否已將您的config.xml中的網址列入白名單?

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

瞭解更多:http://docs.phonegap.com/en/3.0.0/guide_appdev_whitelist_index.md.html#Domain%20Whitelist%20Guide

+0

嗨...我是新來的電話差距,我沒有config.xml,而是我的res/xml文件夾中有plugins.xml和cordova.xml。那麼我應該把這個放在哪裏? – user2801426

+0

查看鏈接,如果您在本地構建項目,這包括每個平臺的說明:http://docs.phonegap.com/en/3.0.0/guide_appdev_whitelist_index.md.html#Domain%20Whitelist%20Guide – pppontusw

+0

謝謝了。現在工作:) – user2801426

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() { 

     }) 
+0

非常感謝:) :) – user2801426