简体   繁体   English

GET 请求在前端反复失败,但在后端没有

[英]GET Request repeatedly failed on the front end but not on backend

I'm working on a personal project that will allow users to find new books based on their preferences for the genre.我正在从事一个个人项目,该项目将允许用户根据他们对流派的偏好来查找新书。 The database I'm using is MongoDB.我使用的数据库是 MongoDB。 However, while I'm able to get all the data on the backend using Postman, I can't get it properly displayed on the frontend.但是,虽然我能够使用 Postman 在后端获取所有数据,但我无法在前端正确显示它。 At the moment, I'm just trying to get the data sent to the front end and at least console.log'd but it isn't making it that far.目前,我只是试图将数据发送到前端,至少 console.log'd 但它并没有那么远。

Here is the code in the routes file.这是路由文件中的代码。

  router.get('/books/:genre', bookBuilder.get_some_books)

Here's the code on the backend that the routes file is pointing to and is working:这是路由文件指向并正在工作的后端代码:

exports.get_some_books = async function (req, res) {
  let { genre } = req.params;
  try {
    let books = await Book.find({"genre": genre});
    if (books) {
      res.json(books)
    } else {
      res.status(404).send({error: 'Not Found'});
    }
  } catch (err) {
    res.status(500).send({error: err.message});
  }
}

Here's my code on the frontend that is not working.这是我在前端的代码不起作用。

    async getEverything() {
      try {
        let pbBooks = await axios.get(`/books/`, {
          method: 'GET',
          headers: {'Content-Type': 'application/json'},
          params: {
            genre: 'PB'
          }
        })
        if (pbBooks) {
          console.log(pbBooks)
        } else {
          this.$router.push('/Error');
        }
      } catch (err) {
        console.log(`Network error: ${err.message}`)
      }
    }

My code stack is Vue.js, Express.js, Node.js and Axios.我的代码堆栈是 Vue.js、Express.js、Node.js 和 Axios。 On the frontend, I've tried making the inner code of axios.get() into '/books/PB' and then tried getEverything(genre) along with /books/${genre} but neither seems to be working.在前端,我尝试将 axios.get() 的内部代码转换为 '/books/PB',然后尝试 getEverything(genre) 和/books/${genre}但似乎都不起作用。

The error I am getting is a 404 Request Failed error that is from the catch block in the getEverything() function.我收到的错误是 404 Request Failed 错误,它来自 getEverything() function 中的 catch 块。 I'm not sure why the frontend is unable to get the data when the backend works just fine.我不确定为什么当后端工作正常时前端无法获取数据。 Is there anything I'm doing wrong?有什么我做错了吗?

Change改变

let pbBooks = await axios.get(`/books/`, {
...

to

let genre = "PB"

let pbBooks = await axios.get(`/books/${genre}`, {
  method: 'GET',
  headers: {'Content-Type': 'application/json'}
})

reason is the params part of the config object is converted to query strings ( /books?genre=PB ) instead of /books/PB, which is what the backend is expecting原因是配置 object 的参数部分被转换为查询字符串( /books?genre=PB )而不是/books/PB, which is what the backend is expecting

More: https://masteringjs.io/tutorials/axios/get-query-params更多: https://masteringjs.io/tutorials/axios/get-query-params

404 is the HTTP status code for Not found , which implies there is no route setup on localhost for /books . 404Not found的 HTTP 状态代码,这意味着本地主机上没有/books的路由设置。 Actually, /books would route to your app, not the backend (unless you have a proxy setup on your app server that redirects to the backend).实际上, /books会路由到您的应用程序,而不是后端(除非您在应用程序服务器上设置了重定向到后端的代理)。

If a proxy were involved, it's likely misconfigured.如果涉及代理,则可能配置错误。 Otherwise, the target URL in the Axios request should be <backend_url>/books (eg, http://localhost:9999/books with the back running locally on port 9999, and the app on some other port).否则,Axios 请求中的目标 URL 应该是<backend_url>/books (例如, http://localhost:9999/books

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

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