我在引用此代碼塊中的「url」時不斷收到此錯誤。 Uncaught ReferenceError:url未定義。儘管在ajax上面的變量中明確定義了URL。我究竟做錯了什麼?未捕獲的ReferenceError:url未定義
$.ajax({
url: url,
dataType: 'jsonp',
cache: true,
jsonpCallback: 'wCallback_1'
});
下面是完整的代碼
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(function() {
// Specify the location code and units (f or c)
var location = 'SPXX0550';
var u = 'c';
// Run the query (pull data from rss feed)
var query = 'SELECT * FROM rss WHERE url="http://xml.weather.yahoo.com/forecastrss/' + location + '_' + u + '.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['wCallback_1'] = function(data) {
var info = data.query.results.item.forecast[0];
$('#wIcon').append('<img src="http://l.yimg.com/a/i/us/we/52/' + info.code + '.gif" width="34" height="34" title="' + info.text + '" />');
$('#wTemp').html(info.temp + '°' + (u.toUpperCase()));
$('#wText').html(info.text);
};
$.ajax({
url: url,
dataType: 'jsonp',
cache: true,
jsonpCallback: 'wCallback_1'
});
你有你的Ajax調用你的準備函數的範圍之外。所以ajax調用會在文檔準備就緒之前嘗試執行,並將'url'變量渲染爲未定義的,因爲它是在執行文檔就緒狀態時編譯的。 – Ohgodwhy 2013-02-21 21:40:31
''''''''''''就緒''回調是**本地**。你爲什麼不把所有的代碼放在回調中?此外,在執行'$ .ajax'時,'ready'回調函數尚未被調用。 – 2013-02-21 21:41:10