gpt4 book ai didi

javascript - 无法获得 GET 请求 axios 的响应

转载 作者:行者123 更新时间:2023-11-29 05:55:04 27 4
gpt4 key购买 nike

我从 OpenWeather API 调用 GET 请求,如下所示:

const config = {
headers: {
accept: 'application/json',
},
data: {},
};
const url = 'http://samples.openweathermap.org/data/2.5/forecast?id=524901&appid=b6907d289e10d714a6e88b30761fae22';
Axios.get(url, config).then((response) => {
console.log(response);
}).catch((e) => {
Alert.alert('error', `${e}`);
});

但是 .then() 没有被调用,大约 15 秒后它抛出异常并显示 catch 警报(显示 网络错误而其他 API 调用正在运行,因此网络错误并不是真正的问题)。

该 url 在浏览器中有效,但 Axios 无法进行该调用。有人知道发生了什么事吗?

最佳答案

确保您需要将 NPM 包放在顶部:

对于节点/ react :

const axios = require('axios');

import axios from 'axios';

您提供的代码对我有用。我只是安装了 axios,导入它,并在您的示例中小写“axios”,然后我收到了气象服务的响应。

我收到以下 JSON 数据响应(以及一些其他内容):

 data:
{ cod: '200',
message: 0.0036,
cnt: 40,
list:
[ [Object],
...
[Object],
[Object] ],
city:
{ id: 524901, name: 'Moscow', coord: [Object], country: 'none' } } }

关于javascript - 无法获得 GET 请求 axios 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55248476/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com