在JSON调用中使用变量URL,不起作用?

时间:2017-02-27 20:29:30

标签: javascript jquery json

我正在尝试创建一个简单的天气应用程序,似乎我无法使用我的变量为JSON链接访问任何信息。继承我的代码:

$(document).ready(function() {
    var key = 'a91a892f1f2a1aa3f7409a78f72af675';
    var locationURL = 'http://ip-api.com/json';
    var longitude;
    var latitude;

    //Getting the geolocation from the user
    $.getJSON(locationURL, function(data) {
        longitude = data.lon;
        latitude = data.lat;
        var url = 'api.openweathermap.org/data/2.5/weather?lat=' + latitude + '&lon=' + longitude + '&appid=' + key;
        console.log(url);
        $.getJSON(url, function(data2){
            console.log(data2, url);
        });

    });
});

但是我从第二次JSON调用中得不到回报,似乎变量'url'被破坏了。我做错了什么或者没有看到这里?

2 个答案:

答案 0 :(得分:3)

您缺少协议:

var url = '//api.openweathermap.org/data/2.5/weather?lat=' + latitude + '&lon=' + longitude + '&appid=' + key;

答案 1 :(得分:0)

get: function(req, data, callback) {
    data.gym_hash = this.gym_hash;
    console.log(data);

    $.ajax({
        url: rest.server + req,
        type: 'GET',
        data: data,

        success: function(result) {
            callback(result);
        }
    });
}

如果代码让我使用这种类型,让js和jQuery将参数格式化为url。电话是这样的:

       get(
        "Path/To/Rest",
        {
            email: email,
            password: password,

        },
        function(result){console.log(result);});