语法错误:JSON.parse:

时间:2019-11-28 21:06:46

标签: javascript json api darksky

您能帮我在这里找到错误吗?

  

SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符

我的代码:

window.addEventListener('load', () => {
    let long; //longitude:
    let lat; //latitude;
    let temperatureDescription = document.querySelector('.temperature-description');
    let temperatureDegree = document.querySelector('.temperature-degree');


    //if location exist in browser
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(position => {
            long = position.coords.longitude;
            lat = position.coords.latitude;


            let proxy = 'https://cors-anywhere.herokuapp.com/';

            //Dark Sky API --- api key is in api address after forecast/
            //on the end of api change numbers for longitude & latitude in variable
            const api = '${proxy}https://api.darksky.net/forecast/09e239664b0eb3d9ee3f2e5e9463217a/${lat},${long}';

            // fetch extract info from const api
            fetch(api)
            // return fetch data to Json 
            .then(response => {
                return response.json();
            })
            .then(data=>{
                    const {temperature, summary } = data.currently;
                });
        });
    }
});

1 个答案:

答案 0 :(得分:0)

我可能会弄错,但是您在api作业中使用了反引号吗?

const api = '${proxy}https://api.darksky.net/forecast/09e239664b0eb3d9ee3f2e5e9463217a/${lat},${long}';

如果使用单引号,则不会插入${proxy}等。

在开发人员工具中检查“网络”标签以确保。