OpenWeatherAPI'den JSON verilerine erişiyorum. URL için doğru bir biçimde
http://api.openweathermap.org/data/2.5/weather?q=Chicago&APPID=34lkj349gga9s8dug9sd8hg
? Q = {şehir} & APPID = {API_KEY}
Ben url, q param ve APPID param sağlamak varsayalım olduğunu. JSON verilerini almak için $ .getJSON işlevini kullanıyorum. $ .getJSON, URL'lerin?, =, Ve & ile biçimlendirildiğini biliyor mu yoksa bunları kendi parametrelerimle mi yazmalıyım? Şu anda tüm iade ettiğim localhost /?
İşte yazdığım kısa program. Çalışmayı nasıl beklediğimi açıklamakta fayda var.
// Here is how the final url should look:
// api.openweathermap.org/data/2.5/weather?q=Chicago&APPID=33lkr3jlfj39asdflk
var weatherSearch = '';
// weather-search is my html form id. On submit, send the input
// (which is city name) to the function getWeather.
$('#weather-search').submit(function(event) {
weatherSearch = $('#weatherQuery').val();
event.preventDefault();
getWeather(weatherSearch);
});
// getWeather has params q (city name), and APPID (API key).
function getWeather(weatherSearch) {
var params = {
q: weatherSearch,
APPID: '33lkr3jlfj39asdflk'
};
// This is the url that goes before the params.
url = 'http://api.openweathermap.org/data/2.5/weather/';
// Request data using url and params above.
// Does $.getJSON format the url properly?
$.getJSON(url. params, function(data) {
// Pass JSON data to showWeather function.
showWeather(data.items);
console.log(data.items);
});
}
function showWeather(weather) {
// Show JSON data (weather) in html div id="weatherResults"
$('#weatherResults').html(weather);
}
Bu, JavaScript tarafından başvurulan html dosyasıdır.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>weather</title>
<script src="/jquery.js"></script>
<script src="openweather.js"></script>
</head>
<body>
<form id="weather-search">
<input type="text" id="weatherQuery"></input>
<input type="submit" value="Submit"></input>
</form>
<div id="weatherResults">
</div>
evet ama javascript dizeleri bitiştirmek 'ile değil .' bu + '->' url ile. params' aslında 'url + params' – Sabbin
POST yöntemi gerektiren sonucu alamıyor. –
@KalpeshSingh bu doğru değil –