Json 来自APIXU的天气API存在问题
我刚刚开始学习API请求,我正在尝试使用APIXU API调用显示多伦多的当前天气(以下是文档:),我不确定我做错了什么 以下是html:Json 来自APIXU的天气API存在问题,json,api,knockout.js,weather-api,Json,Api,Knockout.js,Weather Api,我刚刚开始学习API请求,我正在尝试使用APIXU API调用显示多伦多的当前天气(以下是文档:),我不确定我做错了什么 以下是html: <div class="weather"> <h3>Toronto Weather Forecast</h3> <ul style="margin: 0"> </ul> </div> 多伦多天气预报 以下是js: // Weather API
<div class="weather">
<h3>Toronto Weather Forecast</h3>
<ul style="margin: 0">
</ul>
</div>
多伦多天气预报
以下是js:
// Weather API
function loadData() {
var weatherAPIXU = "http://api.apixu.com/v1/current.json?key=XXXXXXXXXXXXXXXXX&q=Toronto";
$.getJSON(weatherAPIXU, function(data) {
var list = $(".place ul");
forecast = data.current;
list.append('<li>Temp: ' + forecast.temp_c + '°C</li>');
}).error(function(e) {
$(".place").append('<p style="text-align: center;">Sorry!</p><p style="text-align: center;">Could Not Be Loaded</p>');
});
};
$('.place').submit(loadData);
//天气API
函数loadData(){
var Weatherapix=”http://api.apixu.com/v1/current.json?key=XXXXXXXXXXXXXXXXX&q=Toronto";
$.getJSON(weatherAPIXU,函数(数据){
风险值列表=$(“.place ul”);
预测=数据。当前;
列表.附加(“Temp:”+forecast.Temp_c+“°c ”);
}).错误(函数(e){
$(“.place”).append(“抱歉!
无法加载);
});
};
$('.place')。提交(loadData);
一旦我知道如何通过JSON实现它,我也想将它与knockout.js绑定
所以,如果你能给我一些建议,我将不胜感激 我仔细研究了一下,发现我根本没有加载API。因此,对于那些可能有类似问题的人,这里有一个可行的解决方案
$(document).ready(function loadData() {
var weatherAPIXU = "http://api.apixu.com/v1/current.json?key=XXXXXXXXXXXX&q=Toronto";
$.getJSON(weatherAPIXU, function(data) {
var forecast = data.current.temp_c;
var weather = $(".weather");
weather.append(forecast + '° C');
}).error(function(e) {
$(".weather").append('Sorry! Not Loaded');
});
$('.weather').submit(loadData);
});
在这个链接中,它说明了如何使用knockout:处理ajax请求。另外,Knockout映射插件可以帮助您()。感谢您的回复!我还没有进入这方面的映射,但感谢你的链接-我会研究它。现在我只是很好奇为什么我的JSON没有加载到html中。因为我可以在控制台中看到结果。