Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

javascript - 语法错误:JSON.parse:(SyntaxError: JSON.parse:)

Can you help me to find error here?

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

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

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

My code:

(我的代码:)

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;
                });
        });
    }
});
  ask by Драгослав Ивкови? translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I could be mistaken, but are you using backticks in the api assignment?

(我可能会弄错,但是您在api分配中使用了反引号吗?)

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

If you use single quotes, the ${proxy} etc. won't be interpolated.

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

Check your network tab in the Developer Tools to be sure.

(确保在开发人员工具中检查您的网络标签。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...