4

'url'このコード ブロックを 参照すると、このエラーが発生し続けます。

Uncaught ReferenceError: 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'
});

4

3 に答える 3

9

urlで囲まれたコードのブロックを定義して入力するため$(function() { })、ドキュメントが読み込まれるときに実行されます。

ただし、それに続くコード(使用しようとしている場所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'
    });
});
于 2013-02-21T21:41:41.993 に答える
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);
};
于 2013-02-21T21:40:52.410 に答える
0

あなたurlは関数内で定義されているので、その実行コンテキスト(スコープ)にバインドされます。$.ajax呼び出しは同じ実行コンテキストにある必要があります。関数に移動すると、機能します。

于 2013-02-21T21:41:40.140 に答える