javascript - 为什么这个 axios.get 请求不起作用>?
问题描述
我目前正在参加 Colt Steele 的 Web 开发人员训练营,并且遇到了这个问题......
在这个特定的教程中,我们使用 axios 作为“请求”已经停止,所以我试图跟随这个。
我想要做的是为“/results”页面设置一个 Get 路由,在此我想从 OMDB 电影数据库中提取信息,现在,当我进入这个 Url 时,只需显示 JSON 文件。
我确信有一个明显的解决方案,但经过几个小时的搜索,我似乎无法弄清楚。
这是我的代码;
const express = require('express');
const app = express();
const axios = require('axios').default;
app.listen(3000, () => {
console.log('Im listening');
});
app.get('/results', (req, res) => {
axios.get('http://www.omdbapi.com/?t=california&apikey=thewdb')
.then((response) => {
res.send(response);
}).catch((err) => {
console.log('This isnt right');
})
});
如您所见,我也在使用 express,一切都已正确安装。事实上,当我像这样执行 console.log(response) 时:
axios.get('http://www.omdbapi.com/?t=california&apikey=thewdb')
.then((response) => {
console.log(response);
}).catch((err) => {
console.log('This isnt right');
})
它有效,我可以在控制台中看到 API JSON,这让我觉得在 promise 中使用 res.send(response) 存在问题。
任何帮助将不胜感激。抱歉,如果我错过了任何信息,对此还是很陌生...
解决方案
要获取 OMDb 请求的响应数据,请使用data
属性:
app.get('/', function (req, res, next) {
axios.get('http://www.omdbapi.com/?t=california&apikey=thewdb')
.then(result => res.send(result.data))
.catch(err => res.send(err));
});
有关更多信息,请参阅Axios 的响应模式文档。
推荐阅读
- java - 如何将json解析为对象
- python - 尝试使用另一个列表中的索引存储特定值时出现列表索引错误
- python - ValueError:发现样本数量不一致的输入变量:[8000, 1]
- java - 如何将接收到的参数声明为决策表中的全局变量
- c# - 如何在 C# 中保留百分比单元格 NPOI 的数值
- python - 删除连续的重复值python
- jenkins - Jenkins 管道中 .yml 和 .groovy 文件之间的区别
- python - python日志模块跳过一些日志,不写入文件
- amazon-redshift - 我可以继续使用 Aginity Workbench 进行红移吗
- apache-spark - 如何以 JSON 格式转换 debezium 消息,以便可以将其加载到 Redshift