2016-07-12 2 views
0

간단한 웹 API를 사용하여 도시 날씨 정보를 얻고 있습니다 런던. 그러나 필요한 응답을 얻지 못하고 대신 401 오류. 내가 뭐 잘못하고 있니? 다음 API를날씨 API (RESTful) 제공 오류

http://openweathermap.org/current

ERROR를 사용

HTML

<html> 
<head> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $('button').click(function(){ 
     $.ajax({ 
      type: "get", 
      url: "http://api.openweathermap.org/data/2.5/weather?q=London", 
      dataType: "json", 
      contentType: 'application/json', 
      success: function(data) { 
        //What to do on success 
      } 
     }); 
    }); 
}); 
</script> 
</head> 

<body> 
    <button>Click Me</button> 
</body> 

</html> 

enter image description here

+0

해당 호출 : '{ "cod": 401 "message": "유효하지 않은 API 키 .http : // o를 참조하십시오. 자세한 정보는 penweathermap.org/faq#error401을 참조하십시오. "}'요청에 유효한 API 키를 전달하고 있습니까? http://openweathermap.org/faq#error401 –

+0

Pls는 첨부 된 페이지 및 문서를 참조하십시오. 나는 그곳으로부터 URL을 골랐다. – Deadpool

+0

설명서에 사용하려면 API 키가 있어야한다고 나와 있습니다 (http://openweathermap.org/appid). 또한 무료 계정에서는 API 호출 횟수에 따라 요금이 제한된다는 점에 유의하십시오. 귀하의 웹 사이트에이 서비스를 의뢰해야한다면 SLA에 대한 지불을 제안합니다 –

답변

1

weather api의 URL에 쿼리 문자열로 키를 추가해야합니다. 당신을 API에 대한 사용자의 통화에서 다음

http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=12345 

:이 API를 필요로하는 형식의 키를 반환합니다

var url = 'http://api.openweathermap.org/data/2.5/forecast/city'; 
var keys = { 
    id: 524901, 
    APPID: 12345 // Put your key here 
}; 

function makeUrl (url, queryStringObject) { 
    var query = []; 
    // Loops through each key 
    for(var key in queryStringObject){ 
     query.push(encodeURIComponent(key) + '=' + 
      encodeURIComponent(queryStringObject[key])); 
    } 
    // Returns the url with the keys appended 
    return url + (query.length ? '?' + query.join('&') : ''); 
} 

:

이 작업을 수행 할 수있는 작은 기능을 추가 할 수 있습니다 함수 호출을 사용할 수 있습니다 :