未捕获的ReferenceError: url未定义。

时间:2021-10-27 19:07:31

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

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

3 个解决方案

#1


8  

Because you define and populate url in the block of code surrounded by $(function() { }), that runs when the document is loaded.

因为在代码块中定义和填充url(函数(){}),在加载文档时运行该url。

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

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

#2


0  

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

#3


0  

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调用位于相同的执行上下文中。如果你把它移到函数中,它就会起作用。

#1


8  

Because you define and populate url in the block of code surrounded by $(function() { }), that runs when the document is loaded.

因为在代码块中定义和填充url(函数(){}),在加载文档时运行该url。

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

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

#2


0  

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

#3


0  

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调用位于相同的执行上下文中。如果你把它移到函数中,它就会起作用。