API调用在jquery中不起作用

API调用在jquery中不起作用,jquery,Jquery,我正在努力创建一个web应用程序来显示当地的天气。我首先调用navigator.geolocation来获取经度和纬度,然后使用下面的链接进行API调用并获取响应 现在尝试获取响应,稍后将转换为html。但什么也得不到。请帮忙 我的密码笔- 您应该使用以下内容: $.getJSON('api.openweathermap.org/data/2.5/weather?lat='+latitude+'&lon='+longitude, function (json) { 否则,将纬度和经

我正在努力创建一个web应用程序来显示当地的天气。我首先调用navigator.geolocation来获取经度和纬度,然后使用下面的链接进行API调用并获取响应

现在尝试获取响应,稍后将转换为html。但什么也得不到。请帮忙

我的密码笔-


您应该使用以下内容:

$.getJSON('api.openweathermap.org/data/2.5/weather?lat='+latitude+'&lon='+longitude, function (json) {

否则,将纬度和经度作为字符串传递,而不是它们的值

$.getJSON('api.openweathermap.org/data/2.5/weather?lat='+latitude+'&lon='+longitude, function (json) {
var baseUrl = "//api.openweathermap.org/data/2.5/weather?";
var lat = "lat="+ latitude;
var lan = "&lon="+ longitude;

$.getJSON(baseUrl+lat+lan, function(json){
 $(".message").html(JSON.stringify(json));
})

否则,您将以字符串形式传递纬度和经度,而不是它们的值。此API需要密钥-

var baseUrl = "//api.openweathermap.org/data/2.5/weather?";
var lat = "lat="+ latitude;
var lan = "&lon="+ longitude;

$.getJSON(baseUrl+lat+lan, function(json){
 $(".message").html(JSON.stringify(json));
})

}))

早点声明变量。此API需要密钥-

}))

2件事:

  • 获取适合API的经度和纬度变量
  • 您的api调用正在使用相对路径,请尝试使用绝对链接调用它
试试这个代码

$(document).ready(function () {
var latitude = 0;
var longitude = 0;
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    latitude = position.coords.latitude;
                    longitude = position.coords.longitude;
                });
            }

            $("#getTemp").on("click", function () {
        var url = "http://api.openweathermap.org/data/2.5/weather?lat="+latitude +"&lon="+longitude ;
                $.getJSON(url , function (json) {
                    $(".message").html(JSON.stringify(json));
                });
            });
});
2件事:

  • 获取适合API的经度和纬度变量
  • 您的api调用正在使用相对路径,请尝试使用绝对链接调用它
试试这个代码

$(document).ready(function () {
var latitude = 0;
var longitude = 0;
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    latitude = position.coords.latitude;
                    longitude = position.coords.longitude;
                });
            }

            $("#getTemp").on("click", function () {
        var url = "http://api.openweathermap.org/data/2.5/weather?lat="+latitude +"&lon="+longitude ;
                $.getJSON(url , function (json) {
                    $(".message").html(JSON.stringify(json));
                });
            });
});

通过使用天气api中的appid获得正确的结果

$(document).ready(function () {
var latitude;
var longitude;
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
    });
}

$("#getTemp").on("click", function () {
var appid = 'xxxxxxxxxxxxxxxxxxxxxxx';
var url = "api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid" + appid;
    $.getJSON(url, function (json) {
    $(".message").html(JSON.stringify(json));
});
});

通过使用天气api中的appid获得正确的结果

$(document).ready(function () {
var latitude;
var longitude;
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
    });
}

$("#getTemp").on("click", function () {
var appid = 'xxxxxxxxxxxxxxxxxxxxxxx';
var url = "api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid" + appid;
    $.getJSON(url, function (json) {
    $(".message").html(JSON.stringify(json));
});
});

为什么要在操作之前声明它呢?它都在同一个范围内。这里不需要全局变量,谢谢大家的帮助。但它仍然不起作用。我没有得到API的内容。谁能看看我的钢笔吗。为什么要在操作之前声明它呢?它都在同一个范围内。这里不需要全局变量,谢谢大家的帮助。但它仍然不起作用。我没有得到API的内容。谁能看看我的钢笔吗@nabs82请向我们显示console.log的纬度和经度变量结果好吗?@nabs82请向我们显示console.log的纬度和经度变量结果好吗?