代码之家  ›  专栏  ›  技术社区  ›  Red Rabbit

解决React JS中的API调用问题(400错误)[重复]

  •  0
  • Red Rabbit  · 技术社区  · 6 年前

    我正在使用ReactJS和地理位置(导航器)API和Darksky天气API构建天气应用程序。我得到了经度和纬度显示正确,但当我试图检索数据时,我得到了 代码:400,错误:“给定位置(或时间)无效。” . 但是,如果我手动将地址放入浏览器,我会得到正确的JSON返回。

    URL请求示例: https://api.darksky.net/forecast/ API U密钥/37.4498,-77.3047

    查看控制台中的标题,它甚至没有显示请求的URL包含我传入的经度和纬度。可能是在我得到纬度和经度之前执行了天气API调用吗?

    每个控制台的请求URL: https://api.darksky.net/预测/ api_秘密_密钥/,

      getWeather = async (e) => { //Get weather data
        let latitude = '';
        let longitude = '';
        e.preventDefault(); //prevent page reload behaviour
        if ("geolocation" in navigator) {
          navigator.geolocation.getCurrentPosition((position) => {
            latitude = position.coords.latitude.toFixed(4); //save lat and lon from above API call
            longitude = position.coords.longitude.toFixed(4);
    
            console.log("Latitude: " + latitude + ", Longitude: " + longitude);
    
          });
        } else {
          console.log("Geolocation not available");
        }
    
        //Pass the lattitude and longitude values from the location API call to the weather API
        const weather_api_call = await fetch(`https://api.darksky.net/forecast/${api_secret_key}/${latitude},${longitude}`);
        const weather_data = await weather_api_call.json(); //retrieve weather API data
    
        console.log(weather_data); //print weather API data to console
      }
    

    答:最终将气象API数据的获取和记录移动到getcurrentPosition函数中

      getWeather = (e) => {
        e.preventDefault(); //prevent page reload behaviour
    
        if ("geolocation" in navigator) { //if the users allows geolocation to be active
          navigator.geolocation.getCurrentPosition(async (position) => {  //access naviagotr API & get the users current lat and lon
    
            let latitude = position.coords.latitude.toFixed(4); //save lat and lon from above API call
            let longitude = position.coords.longitude.toFixed(4);
    
             console.log("Latitude: " + latitude + " Longitude: " + longitude); //check lat and lon 
    
              //Pass the lattitude and longitude values from the location API call to the weather API
              const weather_api_call = await fetch(`https://api.darksky.net/forecast/${api_secret_key}/`+latitude+`,`+longitude); //run API call after when lat and lon data is gotten
              const weather_data = await weather_api_call.json(); //retrieve weather API data after the call is executed with the lat and lon
    
              console.log(weather_data); //print weather API data to console
    
          });
        } else {
          console.log("Geolocation not available"); //Log if user blocks location in browser
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Dennis Ruiter    6 年前

    navigator.geolocation.getCurrentPosition 是一个异步调用,您在其中设置所提供回调中的纬度和经度值。然而, getWeather 函数只继续执行并使用定义为空字符串的原始纬度和经度值调用fetch。

    如果将fetch调用移入 导航器.地理位置.getcurrentposition 如果您确定定义了纬度和经度,应该可以。