Javascript 使用$.getjson从externel源请求JSON。成功在哪里?

Javascript 使用$.getjson从externel源请求JSON。成功在哪里?,javascript,jquery,Javascript,Jquery,我正在尝试从openweathermap获取天气数据。这个url使用我输入的坐标,当我在浏览器栏中输入url时,我可以下载JSON。我正在努力让这个在我的页面上运行。当我运行这段代码时,在Firebug中,我可以看到HTTP请求获得了200个成功代码,但出于某种原因,它没有打印响应。我没有正确使用getJSON吗 var url = "http://api.openweathermap.org/data/2.5/forecast?lat="+ position.coords.latitude +

我正在尝试从openweathermap获取天气数据。这个url使用我输入的坐标,当我在浏览器栏中输入url时,我可以下载JSON。我正在努力让这个在我的页面上运行。当我运行这段代码时,在Firebug中,我可以看到HTTP请求获得了200个成功代码,但出于某种原因,它没有打印响应。我没有正确使用getJSON吗

var url = "http://api.openweathermap.org/data/2.5/forecast?lat="+ position.coords.latitude +"&lon=" + position.coords.longitude; 

$.getJSON(url, function(res) {
console.log(res);
});  

您试图在一个读取JSONP的函数中读取跨域JSON。 无法跨域读取JSON

改为尝试JSONP请求;,通过追加回调

    var url = "http://api.openweathermap.org/data/2.5/forecast?lat=" + 
position.coords.latitude +"&lon=" + position.coords.longitude + "&callback=?" ; 

    $.getJSON(url, function(res) {
    console.log(res);
    });  
JSON响应如下所示:
{'a':22}

JSONP响应类似于:
myFunction({'a':22})
,其中myFunction是作为
回调传递的值

jQuery不需要回调函数的名称,但是需要在URL中提到
callback
,以便将其标识为JSONP请求

JSONP

如果URL包含字符串“callback=?”(或类似字符串,如 服务器端API),请求被视为JSONP。见 有关更多详细信息,请讨论$.ajax()中的jsonp数据类型


您试图在一个读取JSONP的函数中读取跨域JSON。 无法跨域读取JSON

改为尝试JSONP请求;,通过追加回调

    var url = "http://api.openweathermap.org/data/2.5/forecast?lat=" + 
position.coords.latitude +"&lon=" + position.coords.longitude + "&callback=?" ; 

    $.getJSON(url, function(res) {
    console.log(res);
    });  
JSON响应如下所示:
{'a':22}

JSONP响应类似于:
myFunction({'a':22})
,其中myFunction是作为
回调传递的值

jQuery不需要回调函数的名称,但是需要在URL中提到
callback
,以便将其标识为JSONP请求

JSONP

如果URL包含字符串“callback=?”(或类似字符串,如 服务器端API),请求被视为JSONP。见 有关更多详细信息,请讨论$.ajax()中的jsonp数据类型


将此
?回调=?
附加到url,然后重试,如下所示:

$.getJSON(url + '?callback=?', function(res) {
    console.log(res);
});

将此
?回调=?
附加到url,然后重试,如下所示:

$.getJSON(url + '?callback=?', function(res) {
    console.log(res);
});
试试这个

 function buildQuery() {
     var str = "http://api.openweathermap.org/data/2.5/forecast?lat=27.175009&lon=78.041849";
            return "select * from json where url ='" + str + "' ";
        }

        $.ajax({
            url: 'http://query.yahooapis.com/v1/public/yql',
            data: {
                q: buildQuery(),
                format: "json"
            },
            dataType: "jsonp",
            success: function (data) {
                alert(JSON.stringify(data));
            },
            error: function (data) {
                consol.log(data);
            }
      });
工作演示:-

试试这个

 function buildQuery() {
     var str = "http://api.openweathermap.org/data/2.5/forecast?lat=27.175009&lon=78.041849";
            return "select * from json where url ='" + str + "' ";
        }

        $.ajax({
            url: 'http://query.yahooapis.com/v1/public/yql',
            data: {
                q: buildQuery(),
                format: "json"
            },
            dataType: "jsonp",
            success: function (data) {
                alert(JSON.stringify(data));
            },
            error: function (data) {
                consol.log(data);
            }
      });
工作演示:-