简体   繁体   English

为什么这个 axios.get 请求不起作用>?

[英]Why is this axios.get request not working>?

I am currently doing Colt Steele's Web Developer bootcamp and have came across this issue...我目前正在做 Colt Steele 的 Web 开发人员训练营并且遇到了这个问题......

In this particular tutorial we are using axios as 'request' has since become discontinued so I'm trying to follow along with this.在这个特定的教程中,我们使用 axios 因为“请求”已经停止,所以我试图跟随这个。

What I want to do is set up a Get route for a '/results' page, in this I want to pull information from the OMDB Movie Database and just now, simply show the JSON file when I go onto this Url.我想要做的是为“/results”页面设置获取路径,在此我想从 OMDB 电影数据库中提取信息,刚才,当我将 go 显示到此 Z02A3A3A3577012FB59DFEDED74 上时,只需显示 JSON 文件

I'm sure there's an obvious solution but I can't seem to figure it our after having searched for hours.我确信有一个明显的解决方案,但经过几个小时的搜索,我似乎无法弄清楚。

Here is my code;这是我的代码;

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');
        })

});

As you can see I am also using express, everything is installed correctly.如您所见,我也在使用 express,一切都已正确安装。 In fact, when I do a console.log(response) like so:事实上,当我像这样执行 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');
        })

It works, I can see the API JSON in my console, which makes me think there is a problem with using res.send(response) in the promise.它有效,我可以在我的控制台中看到 API JSON,这让我觉得在 promise 中使用 res.send(response) 存在问题。

Any help would be greatly appreciated.任何帮助将不胜感激。 Apologies if I've missed any info out, still fairly new to this...抱歉,如果我错过了任何信息,对此还是很陌生...

To get response data for a OMDb request use data property:要获取 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));
});

For more information, see Axios's Response Schema documentation .有关更多信息,请参阅Axios 的响应模式文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM