[英]retrieving data from API using JQuery/Ajax
我正在嘗試將openweathermap api中的信息獲取到我的應用程序中。 我嘗試使用ajax獲取數據,但顯示為未定義。
$(function(){
var $temp = $('#temperature');
var $humidity = $('#humidity')
temp = document.getElementById("temperature");
$.ajax({
type: 'GET',
url: 'http://api.openweathermap.org/data/2.5/weather?q={' +
markers["title"] + '}&APPID=' + APPID,
success: function(data){
console.log('success', data);
$temp.append(data.main.temp);
$humidity.append(data.main.humidity);
}
});
});
var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">' + markers["title"] + '</h1>'+
'<div id="bodyContent">'+
'<div class = "temperature"><span id = "temperature"> '+ temp +' </span>°C</div>' +
'<div class = "humidity">'+ humidity +'<span id = "humidity"></span>%' +
'</div>';`
您需要JSON.parse()您的數據。 $ ajax可能返回一個字符串,因此您需要先解析數據,然后才能獲取其屬性。
出現未定義錯誤的原因是因為無法檢索字符串的屬性。
按地理坐標:
var apiUrl = "http://api.openweathermap.org/data/2.5/find?lat=" + latitude + "&lon=" + longitude + "&units=imperial&cnt=7&appid=" + APIkey;
使用地理位置來提供緯度和經度的值。
或只是提供城市ID
var apiUrl = 'http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=' + APIkey;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.