我在使用API,特别是Forecast.io天气api创建一个完整的应用程序时遇到了一些问题。为了简单起见,我将JS直接放在HTML页面中。对于这个基本版本,我很高兴有这个节目。假设我想要当前温度(目前是->温度)。而且,我也不确定“?回电话?”总是推荐用于所有RESTful API。
<!DOCTYPE html>
<html>
<body>
<p id="weather">Here's the weather:<p>
<button onclick="b()">Submit</button>
<script>
function b(){
var apiKey = '<private>';
var url = 'https://api.forecast.io/forecast/';
var lati = 0;
var longi = 0;
var data;
$.getJSON(url + apiKey + "/" + lati + "," + longi + "?callback=?", function(data) {
$('#weather').innerHTML('and the weather is: ' + data[4].temperature);
});
}
</script>
</body>
</html>发布于 2013-08-08 18:37:09
您所犯的主要错误是不包括jQuery :-)下一个错误是,在jQuery对象上,您需要使用html()函数而不是JavaScript本机innerHTML属性。
如果使用console.log(数据),您可以看到返回对象的所有属性,因此可以像data.currently.temperature一样正确地引用它
<!DOCTYPE html>
<html>
<body>
<p id="weather">Here's the weather:<p>
<button onclick="b()">Submit</button>
<script>
function b(){
var apiKey = '<PRIVATE>';
var url = 'https://api.forecast.io/forecast/';
var lati = 0;
var longi = 0;
var data;
$.getJSON(url + apiKey + "/" + lati + "," + longi + "?callback=?", function(data) {
//console.log(data);
$('#weather').html('and the temperature is: ' + data.currently.temperature);
});
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</body>
</html>https://stackoverflow.com/questions/18132790
复制相似问题