javascript - 在 getJSON jquery 中使用变量作为 URL

标签 javascript jquery json api

我正在使用 jquery 创建一个天气应用程序。我遇到的问题是在 getJSON 中,我想使用我使用当前纬度和经度坐标创建的变量。带有我的 openweather api key 的 url 在浏览器中加载 json 文件效果很好,但它似乎不想在我的 getJSON 中工作。

这是我的 JavaScript 代码,用于根据您的位置显示天气信息。

var lat;
var lon;
var jsonURL;

  navigator.geolocation.getCurrentPosition(GetLocation);
function GetLocation(location) {
    lat = location.coords.latitude;
    lon = location.coords.longitude;

    jsonURL = 'api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&APPID=example&units=imperial'

}



$("#getWeather").click(function(){
  console.log(jsonURL);
    $.getJSON(jsonURL , function( data ) {
    var items = [];
    items = data;   $("#weather").text(items.main.temp).append('℉');
    });
});

最佳答案

使用浏览器中的开发人员工具。查看控制台。查看网络选项卡。这将清楚地表明您的网址导致了 404 错误。

您的 URL 缺少方案。前面需要 http://https://

当您在地址栏中键入 URL(缺少方案)时,浏览器将自动插入 http://

关于javascript - 在 getJSON jquery 中使用变量作为 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35564241/

相关文章:

python - 加载 JSON 并获取某些数据 (Python)

javascript - 右键单击 'Save as'事件问题

javascript - 什么是选择器引擎?

javascript - Mongoose 多重表达错误?

javascript - 在 jQuery 中获取元素的 HTML

jquery - 仅当元素具有特定属性时,如何使用 jquery 应用 css?

javascript - flex 元素是否有可能与它们上方的元素紧密对齐?

javascript - 使用 Browserify 在 Node JS 项目中包含 JQuery Mobile

javascript - JSON中如何在JS中显示表中的所有数据

javascript - 如何将对象(json)与模型(javascript类)进行比较