jQuery 未捕获的 ReferenceError: url 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15012721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught ReferenceError: url is not defined
提问by ServerSideSkittles
I keep getting this error at referring to 'url'
in this block of code.
'url'
在此代码块中引用时,我不断收到此错误。
Uncaught ReferenceError: url is not defined.
未捕获的 ReferenceError: url 未定义。
Although the URL is defined clearly in a variable above the ajax. What am I doing wrong?
虽然在ajax上面的一个变量中明确定义了URL。我究竟做错了什么?
$.ajax({
url: url,
dataType: 'jsonp',
cache: true,
jsonpCallback: 'wCallback_1'
});
Here is the full code
这是完整的代码
<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'
});
回答by Reinstate Monica Cellio
Because you define and populate url
in the block of code surrounded by $(function() { })
, that runs when the document is loaded.
因为您url
在由 包围的代码块中定义和填充$(function() { })
,它会在加载文档时运行。
However, the code following it (where you try to use url
) is run immediately (before the document has loaded).
但是,它后面的代码(您尝试使用的地方url
)会立即运行(在文档加载之前)。
Just put all the code inside the $(function() { })
block and it will work fine...
只需将所有代码放在$(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 + '°' + (u.toUpperCase()));
$('#wText').html(info.text);
};
$.ajax({
url: url,
dataType: 'jsonp',
cache: true,
jsonpCallback: 'wCallback_1'
});
});
回答by Juan Mendes
Your url
is outside of the scope of your $.ajax
call. You need to move it inside the ready handler, or make url available as a global
您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 + '°' + (u.toUpperCase()));
$('#wText').html(info.text);
};
回答by matt snider
Your url
is defined inside a function so it is bound to that execution context (scope). You need the $.ajax
call to be in the same execution context. If you move it into the function, then it will work.
您url
是在函数内部定义的,因此它绑定到该执行上下文(范围)。您需要$.ajax
调用位于相同的执行上下文中。如果您将其移动到函数中,那么它将起作用。