Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获引用错误:未定义url_Javascript_Ajax_Jquery - Fatal编程技术网

Javascript 未捕获引用错误:未定义url

Javascript 未捕获引用错误:未定义url,javascript,ajax,jquery,Javascript,Ajax,Jquery,我在引用这段代码中的“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.

我在引用这段代码中的“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'
});
您的url超出了$.ajax调用的范围。您需要将其移动到就绪处理程序中,或者使url作为全局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);
};

url是在函数中定义的,因此它绑定到该执行上下文范围。您需要将$.ajax调用置于相同的执行上下文中。如果您将其移动到函数中,那么它将工作。

因为您在加载文档时运行的$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'
    });
});

您的ajax调用超出了ready函数的范围。因此,ajax调用尝试在文档准备就绪之前执行,将url变量呈现为未定义,因为它是在执行document ready状态时编译的。url是ready回调的本地变量。为什么不把所有的代码都放在回调函数中呢?另外,在您执行$.ajax时,还没有调用ready回调。感谢您的解释和回答。作为Javascript新手,这不仅解决了这个问题,而且也是我一直犯的一个常见错误!谢谢不用担心-很乐意帮助mate: