$ .getJSON没有做任何事情

时间:2012-04-29 22:53:04

标签: javascript jquery json

我不确定为什么,但似乎当我在调用另一个getJson之后调用$ .getJSON时,没有任何反应。这是代码:

getWeather();

function getWeather() {
    $.getJSON("http://where.yahooapis.com/geocode?q=" + lat + ",+" + lon + "&gflags=R&flags=J", function(data){
        zipCode = data.ResultSet.Results[0].postal;
        WOEID = data.ResultSet.Results[0].woeid;
        getYahooWeather(WOEID);         
    });
}

function getYahooWeather(x) {
    var query = escape('select item from weather.forecast where woeid="'+x+'"');
    var url = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&callback=c";
    console.log(url);

    $.getJSON(url, function(data2){
        console.log("hey");
    });
}

我的问题是,我是否对这些$ .getJSON调用做错了什么?

非常感谢

2 个答案:

答案 0 :(得分:3)

您已指定回调应为c函数,因此请声明:

function getYahooWeather(x) {
  var query = escape('select item from weather.forecast where woeid="'+x+'"');
  var url = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&callback=c";
  console.log(url);

  $.getJSON(url);
}

function c(data2) {
  console.log("hey");
}

答案 1 :(得分:1)

您的请求不在当前域中。您无法提出外国请求,受跨域政策的限制。

此类请求使用 jsonp 请求代替。这是guide,可以帮助您入门。

相关问题