2013-02-21 44 views
2

我在引用此代碼塊中的「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 + '&deg;' + (u.toUpperCase())); 
    $('#wText').html(info.text); 
}; 

$.ajax({ 
    url: url, 
    dataType: 'jsonp', 
    cache: true, 
    jsonpCallback: 'wCallback_1' 
}); 

+0

你有你的Ajax調用你的準備函數的範圍之外。所以ajax調用會在文檔準備就緒之前嘗試執行,並將'url'變量渲染爲未定義的,因爲它是在執行文檔就緒狀態時編譯的。 – Ohgodwhy 2013-02-21 21:40:31

+0

''''''''''''就緒''回調是**本地**。你爲什麼不把所有的代碼放在回調中?此外,在執行'$ .ajax'時,'ready'回調函數尚未被調用。 – 2013-02-21 21:41:10

回答

8

因爲您在$(function() { })包圍的代碼塊中定義並填充url,該代碼塊在加載文檔時運行。

但是,它後面的代碼(您嘗試使用url)會立即運行(在文檔加載之前)。

只是把所有的代碼$(function() { })塊內,它會正常工作......

$(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 + '&deg;' + (u.toUpperCase())); 
     $('#wText').html(info.text); 
    }; 

    $.ajax({ 
     url: url, 
     dataType: 'jsonp', 
     cache: true, 
     jsonpCallback: 'wCallback_1' 
    }); 
}); 
+0

感謝您的解釋和答覆。作爲Javascript的新手,這不僅解決了這個問題,而且也是我一直在犯的一個常見錯誤!謝謝! – ServerSideSkittles 2013-02-21 21:59:37

+0

不用擔心 - 很高興幫助伴侶:) – Archer 2013-02-21 22:12:26

0

url是你$.ajax通話的範圍之外。你需要將它移動就緒處理程序中,或提供URL作爲全球

$(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; 

    $.ajax({ 
     url: url, 
     dataType: 'jsonp', 
     cache: true, 
     jsonpCallback: 'wCallback_1' 
    });  
}); 

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 + '&deg;' + (u.toUpperCase())); 
    $('#wText').html(info.text); 
}; 
0

url一個函數內部定義,因此被綁定到執行上下文(範圍)。您需要調用$.ajax才能處於相同的執行上下文中。如果你將它移動到函數中,那麼它將起作用。

相關問題