Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我必须单击搜索按钮两次才能从api获取数据_Javascript_Html_Weather Api - Fatal编程技术网

Javascript 我必须单击搜索按钮两次才能从api获取数据

Javascript 我必须单击搜索按钮两次才能从api获取数据,javascript,html,weather-api,Javascript,Html,Weather Api,第一次单击“搜索”按钮时,会出现两个错误: main.js:68 GET 404(未找到) 未捕获(承诺中)SyntaxError:JSON中位于位置0的意外标记N 但当我第二次点击搜索按钮时,错误消失了。 因此,我必须单击搜索按钮两次才能从API获取数据 index.html <form class="searchForm" method="POST"> <div class="form-div"> <label for="loac

第一次单击“搜索”按钮时,会出现两个错误:

  • main.js:68 GET 404(未找到)

  • 未捕获(承诺中)SyntaxError:JSON中位于位置0的意外标记N

  • 但当我第二次点击搜索按钮时,错误消失了。 因此,我必须单击搜索按钮两次才能从API获取数据

    index.html

    <form class="searchForm" method="POST">
        <div class="form-div">
            <label for="loaction">Enter a location</label>
            <input type="text" class="searchForm-input" id="loaction" placeholder="Location">
            <button type="submit">Search</button>
        </div>
    </form>
    <div class="echo">--</div>
    <div class="location">
        <h1 class="location-timezone">Timezone</h1>
    </div>
    <div class="temperature">
        <div class="degree-section">
            <h2 class="temperature-degree">34</h2>
    
            <span>F</span>
    
        </div>
    </div>
    

    您有两个异步操作,其中第二个需要使用第一个AJAX操作的结果来继续:

    fetch(geocodeURL)
            .then(response => {
                return response.json();
            })
            .then(data => {
    
              let max = data.results[0].geometry.location;
              console.log(max);
    
              let max1 = max.lat+',' + max.lng;
              console.log(max1);
               lat1 = max1; <-- lat1 is used in second AJAX call
              console.log(lat1);
            })
        console.log(geocodeURL);
    
    fetch(geocodeURL)
            .then(response => {
                return response.json();
            })
            .then(data => {
    
              let max = data.results[0].geometry.location;
              console.log(max);
    
              let max1 = max.lat+',' + max.lng;
              console.log(max1);
               lat1 = max1; <-- lat1 is used in second AJAX call
              console.log(lat1);
            })
        console.log(geocodeURL);
    
            const proxy = 'https://cors-anywhere.herokuapp.com/';
            const api = 
            `${proxy}https://api.darksky.net/forecast/aki_key/${lat1}`; // <-- lat1 will be undefined
    
    event.preventDefault();
    const input = document.querySelector('.searchForm-input').value;
    // remove whitespace from the input
    const searchQuery = input.split(' ').join('+');
    // print `searchQuery` to the console
    console.log(searchQuery);
    
    let geocodeURL = `https://maps.googleapis.com/maps/api/geocode/json? 
    address=${searchQuery}&key=api_key`;
    
    fetch(geocodeURL)
                .then(response => {
                    return response.json();
                })
                .then(data => {
    
                    let max = data.results[0].geometry.location;
                    console.log(max);
    
                    let max1 = max.lat+',' + max.lng;
                    console.log(max1);
                    lat1 = max1;
                    console.log(lat1);
    
                    let temperatureDegree = document.querySelector('.temperature- 
                     degree');
                    let locationTimezone = document.querySelector('.location-timezone');
                    let echos = document.querySelector('.echo');
                    echos.textContent = searchQuery;
    
                    const proxy = 'https://cors-anywhere.herokuapp.com/';
                    const api = 
                    `${proxy}https://api.darksky.net/forecast/aki_key/${lat1}`;
    
                    fetch(api)
                        .then(response => {
                            return response.json();
                        })
                        .then(data => {
                            console.log(data);
                            const {temperature} = data.currently;
                            temperatureDegree.textContent = temperature;
    
                            locationTimezone.textContent = data.timezone;
    
                        })
                    }
                })
    console.log(geocodeURL);