我试图使用Yahoo的YQL调用多个函数。我想要展示的是每天的天气。 XML文件有[0]今天[1]明天[2]后一天等等。当我运行代码时,它会查看最后一个被调用的号码,而不会加载其他号码。我在哪里,我错了?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
// javascript will go here
$(function(weatherone){
var query = "select * from rss where url='http://xml.weather.yahoo.com/forecastrss/SPXX0050_f.xml'";
var cacheBuster = Math.floor((new Date().getTime()) / 1200 / 1000);
var url = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(query) + '&format=json&_nocache=' + cacheBuster;
window['wxCallback'] = function(data) {
var info = data.query.results.item.forecast[0];
$('#wxDay').text(info.day);
$('#wxIcon').css({
backgroundPosition: '-' + (61 * info.code) + 'px 0'
}).attr({
title: info.text
});
$('#wxIcon2').append('<img src="http://l.yimg.com/a/i/us/we/52/' + info.code + '.gif" width="74" height="74" title="' + info.text + '" />');
$('#wxTemp').html(info.high + '°' + (u.toUpperCase()));
$('#wxText').html(info.text);
};
$.ajax({
url: url,
dataType: 'jsonp',
cache: true,
jsonpCallback: 'wxCallback'
});
});
$(function(weathertwo){
var query = "select * from rss where url='http://xml.weather.yahoo.com/forecastrss/SPXX0239_f.xml'";
var cacheBuster = Math.floor((new Date().getTime()) / 1200 / 1000);
var url = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(query) + '&format=json&_nocache=' + cacheBuster;
window['wxCallback'] = function(data) {
var info = data.query.results.item.forecast[1];
$('#wxDay').text(info.day);
$('#wxIcon').css({
backgroundPosition: '-' + (61 * info.code) + 'px 0'
}).attr({
title: info.text
});
$('#wxIcon2').append('<img src="http://l.yimg.com/a/i/us/we/52/' + info.code + '.gif" width="74" height="74" title="' + info.text + '" />');
$('#wxTemp').html(info.high + '°' + (u.toUpperCase()));
$('#wxText').html(info.text);
};
$.ajax({
url: url,
dataType: 'jsonp',
cache: true,
jsonpCallback: 'wxCallback'
});
});
</script>
答案 0 :(得分:0)
你做
window['wxCallback'] = function(data) { ... };
$.ajax({ ..., jsonpCallback: 'wxCallback' });
两次,第二次破坏第一次。使用不同的回调名称,所以
window['wxCallback1'] = ...;
$.ajax({ ..., jsonpCallback: 'wxCallback1' });
window['wxCallback2'] = ...;
$.ajax({ ..., jsonpCallback: 'wxCallback2' });