简体   繁体   English

如何从JavaScript中的API中提取数据?

[英]How do I pull data from an API in javascript?

I'm working on a little weather app project, but now that i've changed the code to make the long and lat change depending on you geo location, the button doesn't work. 我正在研究一个小天气应用程序项目,但是现在我已经更改了代码以根据您的地理位置来更改经纬度,因此该按钮不起作用。 When i click it nothing happens, i can't even tell if the API is pulling the data. 当我单击它时,什么也没有发生,我什至无法确定API是否正在提取数据。 Here is my code as well as a codepen I made to fool around with. 这是我的代码以及我制作的代码库。 http://codepen.io/lettda/pen/yaGaLx http://codepen.io/lettda/pen/yaGaLx

$(document).ready(function(){
    $("#reveal").on("click", function(){
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
            lat = position.coords.latitude;
            long = position.coords.longitude;

        var long;
        var lat;
        var api = "http://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+long+"&appid=de61ccfbde0405f57d64dbb53323fccf";

                $.getJSON(api, function(data){
                    $(".message").html(JSON.stringify(data))
                });
            });
        };
    });
});

open your developer tools (in chrome press F12) and have a look into the console: 打开您的开发人员工具(在chrome中按F12键),然后查看控制台:

pen.js:4 getCurrentPosition() and watchPosition() no longer work on insecure origins. pen.js:4 getCurrentPosition()和watchPosition()不再适用于不安全的来源。 To use this feature, you should consider switching your application to a secure origin, such as HTTPS. 要使用此功能,应考虑将应用程序切换到安全来源,例如HTTPS。 See https://.... for more details. 有关更多详细信息,请参见https://....。

it's an simple cross origin problem 这是一个简单的跨源问题

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

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