【发布时间】:2017-05-11 11:41:38
【问题描述】:
我想从此 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();
});
任何帮助将不胜感激。
编辑:- 由于 ajax 中的错误函数,我收到一个警告错误,上面写着“[object Object]”。起初我没有看到错误,因为我阻止了页面上的弹出窗口。
【问题讨论】:
-
您能具体说明您遇到了什么错误吗??
-
@MannanBahelim 我什么也没得到。
-
我用 HttpRequester 试过这个,它返回 http 响应,你需要在你的 DOM 中插入这个
-
@SjaakvBrabant 他正在发送,@Sanjay 你能检查一下
document.getElementById("output").innerHTML = data.source;是否按照文档docs.mashape.com/javascript 工作 -
@SjaakvBrabant 这很容易回答。我实际上花时间帮助人们——你可能想看看我回答了多少万个问题。多么粗鲁!
标签: javascript json ajax api