【问题标题】:$.getJSON not doing anything$.getJSON 什么都不做
【发布时间】:2012-05-09 17:48:05
【问题描述】:

我不确定为什么,但似乎当我在调用另一个 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 调用做错了吗?

非常感谢

【问题讨论】:

  • 检查您的控制台是否存在跨域策略错误
  • 这个问题是否可能与stackoverflow.com/questions/5492838/… 重复?
  • 控制台什么也没说。它应该说嘿。第一个 getJSON 工作正常。第二个不行
  • 这些函数有没有被调用过?为每个人做一个console.log(),在他们的getJSON()之前和之后。检查执行链在哪里中断。
  • 在第一个函数中,控制台会显示zipCode和WOEID。在第二个函数中,它将显示 url。但它没有显示嘿

标签: javascript jquery json


【解决方案1】:

你已经指定回调应该是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");
}

【讨论】:

  • 嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎非常感谢你告诉我!
【解决方案2】:

您的请求在当前域之外。您不能提出外部请求,它受到跨域政策的限制。

此类请求并使用 jsonp 请求代替。这里有一个guide 可以帮助您入门。

【讨论】:

    猜你喜欢
    • 2013-10-25
    • 2019-03-08
    • 2016-12-24
    • 2017-04-18
    • 2011-08-03
    • 2014-05-11
    • 2014-09-10
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多