2016-06-17 8 views
0

私は過去7日間の天気情報を取得しようとしていますが、何も返されません。 URLをブラウザから入力すると、xmlフォーマットコードが返されます。しかし、jQueryコードでHTMLを実行すると、実行されません。私がどこに間違って行ったか教えてください。OpenWeather:データを取得しない

私のコードは次のとおりです。

<!doctype html> 

<html> 

<head> 

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 

    <meta charset="utf-8"> 

    <title>OpenWeatherMap API jQuery Plugin</title> 

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 

    <style> 

     /* for presentation only */ 

     body { 
      font: 16px Arial, Helvetica, sans-serif; 
      margin: 0; 
     } 

     .weather-wrapper { 
      background: skyblue; 
      margin: 5% 0 5% 5%; 
      padding: 40px 5%; 
      float: left; 
      color: white; 
      width: 70%; 
      max-width: 400px; 
     } 

     strong { 
      color: steelblue; 
     } 

     .capitalize { 
      text-transform: capitalize; 
     } 

     .hide { 
      display: none; 
     } 

    </style> 

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <script type="application/javascript"> 

     $(document).ready(function(){ 

      $.getJSON('http://api.openweathermap.org/data/2.5/forecast/daily?q=Hyderabad,IN&mode=xml&appid=c9d49310f8023ee2617a7634de23c2aa',function(result){ 
      console.log(result); 

      }); 


     }); 
    </script> 

</head> 

<body lang="en"> 

<script type="text/javascript"> 
    if (typeof jQuery == 'undefined') { 
     document.write(unescape("%3Cscript src='js/lib/jquery.1.9.1.min.js' type='text/javascript'%3E%3C/script%3E")); 
    } 


</script> 

<script src="js/plugins/openWeather.min.js"></script> 


</body> 

</html> 
+1

getJSON関数でXMLデータを取得しようとしていますか?それは問題だと思われます – Atticweb

答えて

2

getJSON関数はJSONデータを返すように期待しています。ですから、このようなXMLデータを返すことができる機能が必要になります。

$.ajax({ 
    type: "get", 
    url: "http://api.openweathermap.org/data/2.5/forecast/daily?q=Hyderabad,IN&mode=xml&appid=c9d49310f8023ee2617a7634de23c2aa", 
    dataType: "xml", 
    success: function(data) { 
     /* handle data here */ 
    }, 
    error: function(xhr, status) { 
     /* handle error here */ 
    } 
}); 

今、この関数は、XMLデータを返すことができます(dataType: "xml"

ます。また、JSONデータを返すために、URLを変更することができます。 http://api.openweathermap.org/data/2.5/forecast/daily?q=Hyderabad,IN&mode=json&appid=c9d49310f8023ee2617a7634de23c2aa

私はmode=jsonをURLに変更しました

+0

ボタンのクリックなどのようなメソッドを呼び出す必要はありますか?ちょうど私は上記のコードと過去の '$(document).ready(function(){}); 'をコピーして実行しました。それは実行されていません。答えを完全に投稿してください。 –

+1

実行中ですが、succes関数 – Atticweb

+0

のデータで何かをする必要があります。すばらしいです!ありがとう。 –

関連する問題