【问题标题】:axios is not fetching the response dataaxios 没有获取响应数据
【发布时间】:2023-01-12 22:51:49
【问题描述】:

下面是用于通过 axios 获取数据的代码。 当我在浏览器中点击实际 url 时,它会显示响应。

{"location":{"name":"Ipoh","region":"Perak","country":"Malaysia","lat":4.58,"lon":101.08,"tz_id":"Asia/Kuala_Lumpur","localtime_epoch":1673526754,"localtime":"2023-01-12 20:32"},"current":.....



const axios = require("axios");
const weatherResponse = axios.get("https://api.weatherapi.com/v1/current.json?key=11&q=49.37.42.66&aqi=yes", {
headers: { "Content-Type": "application/json" }
}
    );
console.log("aasdasd");
console.log(weatherResponse.data);
console.log("cvghcgh");

【问题讨论】:

  • 我认为您需要 await 请求,因为这是一个 async 电话。

标签: javascript node.js axios


【解决方案1】:

我认为这是一个异步问题。

以下是如何执行此操作的示例:


async function getAxios() {
    const axios = require("axios");
    return await axios.get("https://api.weatherapi.com/v1/current.json?key=11&q=49.37.42.66&aqi=yes", {
        headers: {"Content-Type": "application/json"}
    });
}

console.log("aasdasd");
console.log(getAxios());
console.log("cvghcgh");

回复


{
  "error": {
    "code": 2008,
    "message": "API key has been disabled."
  }
}

我认为我们需要发布一个新的 API 密钥。

我希望它有所帮助。

【讨论】:

    猜你喜欢
    • 2021-07-27
    • 1970-01-01
    • 2021-01-31
    • 2020-03-22
    • 2017-03-28
    • 2019-11-26
    • 2023-03-25
    • 1970-01-01
    • 2022-10-19
    相关资源
    最近更新 更多