【问题标题】:Uncaught ReferenceError: url is not defined未捕获的 ReferenceError:未定义 url
【发布时间】:2013-02-07 09:28:05
【问题描述】:

我在这段代码中引用'url' 时不断收到此错误。

未捕获的引用错误:未定义 url。

虽然 URL 是在 ajax 上面的一个变量中明确定义的。我究竟做错了什么?

$.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'
});

【问题讨论】:

  • 您的 ajax 调用超出了 ready 函数的范围。因此,ajax 调用尝试在文档准备好之前执行,将url 变量呈现为未定义,因为它是在文档就绪状态执行时编译的。
  • urlready 回调的本地。为什么不将所有代码都放在回调中?此外,在您执行$.ajax 时,尚未调用ready 回调。

标签: javascript ajax jquery


【解决方案1】:

因为您在由$(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'
    });
});

【讨论】:

  • 感谢您的解释和回答。作为 Javascript 的新手,这不仅解决了这个问题,而且也是我一直犯的一个常见错误!谢谢!
  • 不用担心 - 很高兴帮助队友:)
【解决方案2】:

您的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);
};

【讨论】:

    【解决方案3】:

    您的url 在函数内部定义,因此它绑定到该执行上下文(范围)。您需要 $.ajax 调用位于相同的执行上下文中。如果你把它移到函数中,那么它就会起作用。

    【讨论】:

      猜你喜欢
      • 2023-01-23
      • 2016-11-03
      • 2011-01-05
      • 2016-01-02
      • 2013-10-06
      • 2016-12-17
      相关资源
      最近更新 更多