2017-05-11 9 views
1

날씨 정보를이 API에서 가져오고 싶지만 이상한 이유로 작동하지 않습니다.weather json API를 사용할 수 없습니다.

mashape API입니다. https://market.mashape.com/fyhao/weather-13

여기

function getWeather() { 
    var lat=null; 
    var lon=null; 
    if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     lat = position.coords.latitude; 
     lon = position.coords.longitude; 
    }); 
    } 
    var url = 
    "http://simple-weather.p.mashape.com/weatherdata?lat=" + 
    lat + 
    "&lng=" + 
    lon; 
    $.ajax({ 
    url: url, 
    type: "GET", 
    dataType: "json", 
    success: function(data) { 
     $(".location").html(
     "<h4>" + 
      data.query.results.channel.location.city + 
      ", " + 
      data.query.results.channel.location.country + 
      "</h4>" 
    ); 
     $(".weather").html(
     "<h4>" + data.query.results.channel.item.condition.text + "</h4>" 
    ); 
     $(".temperature").html(
     "<h4>" + data.query.results.channel.item.condition.temp + "</h4>" 
    ); 
    }, 
    error: function(err) { 
     alert(err); 
    }, 
    beforeSend: function(xhr) { 
     xhr.setRequestHeader(
     "X-Mashape-Authorization", 
     "MYKEY" 
    ); 
    } 
    }); 
} 
$(document).ready(function() { 
    $(".info").addClass("animated pulse"); 
    getWeather(); 
}); 

어떤 도움을 이해할 수있을 것이다, 내가 무엇을 시도했다입니다.

편집 - "[object Object]"라는 경고 오류가 발생합니다. 이는 ajax의 오류 기능 때문입니다. 페이지의 팝업을 차단했기 때문에 처음에는 오류가 표시되지 않았습니다. 사물의

+0

당신이지고 어떤 오류 지정하시기 바랍니다 수 있습니까 ?? –

+0

@ManannBahelim 나는 아무 것도 얻지 못하고있다. –

+0

HttpRequester를 사용하여 HTTP 응답을 반환했지만 DOM에이 코드를 삽입해야합니다. –

답변

4

커플 : 비동기

  • getCurrentPosition()입니다. lat와 lon 변수가 좌표로 설정 될 때까지 $ .ajax 요청은 이미 null lat 및 lon 변수와 함께 전송되었습니다.
  • 이 API는 http가 아니라 https를 원합니다.
  • 헤더가 여기에 X-Mashape-Authorization

X-Mashape-Key과하지 않은 예입니다

function getWeather() { 
    var lat = null; 
    var lon = null; 
    if (navigator.geolocation) { 
     //we are putting everything inside the callback 
     navigator.geolocation.getCurrentPosition(function (position) { 
      lat = position.coords.latitude; 
      lon = position.coords.longitude; 

      var url = 
       "https://simple-weather.p.mashape.com/weatherdata?lat=" + 
       lat + 
       "&lng=" + 
       lon; 
      $.ajax({ 
       url: url, 
       type: "GET", 
       success: function (data) { 
        console.log(data); 
       }, 
       error: function (err) { 
        console.error('error ' + JSON.stringify(err)); 
       }, 
       beforeSend: function (xhr) { 
        xhr.setRequestHeader(
         "X-Mashape-Key", "KEY" 
        ); 
       } 
      }); 

     }); 
    } 
} 
$(document).ready(function() { 

    getWeather(); 
}); 
+0

내 이전 의견을 무시하십시오. 좋은 대답입니다! – Jamiec

관련 문제