2013-02-21 96 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才能处于相同的执行上下文中。如果你将它移动到函数中,那么它将起作用。

相关问题