繁体   English   中英

我正在尝试从API提取JSON文件

[英]I'm trying to fetch a JSON file from an API

因此,我正在为chrome创建一个新的标签扩展名,并希望为用户显示天气。 为此,我将地理位置API用于位置,然后将openweather API用于天气数据。 我遇到的问题是,当我使用fetch方法时,它会在本地(URL)本地查找文件:

chrome-extension://kgimldinjffdncnfpgflgjeaiafjdflp/api.openweathermap.org/data/2.5/weather?lat = 56.692819099999994 +&lon = 12.8946167。

但是我希望它在互联网上查找文件,我该如何解决?

这是我当前正在使用的代码。

var OpenWeather;
if("geolocation" in navigator){
      // check if geolocation is supported/enabled on current browser
      navigator.geolocation.getCurrentPosition(function(position:Position){
        fetch("api.openweathermap.org/data/2.5/weather?lat="+position.coords.latitude+"+&lon="+position.coords.longitude).then(function(response){
            response.json().then(function(data){
                OpenWeather=data;
                alert(OpenWeather);
            });
        });


      });
}

在API网址中添加https ,如下所示:

 fetch("https://api.openweathermap.org/data/2.5/weather?lat="+position.coords.latitude+"+&lon="+position.coords.longitude) 

提取是一个返回响应的承诺。

fetch(url).then(res => res.json()).then(json=> {});
// or
fetch(url).then(function (res) {
   return res.json();
}).then(function (json) {});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM