繁体   English   中英

Axios GET 请求失败,状态码为 404

[英]Axios GET request failed with status code 404

我正在尝试构建(使用vue.js )一个博客/文章应用程序,该应用程序使用axios从我的db.json文件中请求数据,并带有get request 一旦从主文章列表中单击它,我希望它显示该文章的数据==>我使用id prop ,以便它知道要获取哪篇文章的数据。 在这里,我调用了我的 get 请求(get 请求在另一个文件中,请继续阅读)。

ArticleShow.vue
<script>
import APIService from "@/APIService.js";
export default {
  props: ["id"],    // "id" has been passed as a prop when I import this file to its parent
  data() {
    return {
      article: {}
    };
  },
  created() {
    APIService.getArticle(this.id).then(response => {
      this.article = response.data;
    });
  }
};
</script>

这是我使用的 axios GET 请求(在 APIService.js 文件中)

APIService.js
import axios from "axios";

const apiClient = axios.create({
  baseURL: "http://localhost:5000",
  withCredentials: false,
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json"
  }
});

export default {
  getArticle(id) {
    return apiClient.get("/articles/" + id);
  }
}

我认为这段代码可以工作,但在我的浏览器中出现以下错误:

GET http://localhost:5000/articles/undefined 404 (Not Found)
Uncaught (in promise) Error: Request failed with status code 404
    at createError (createError.js?2d83:16)
    at settle (settle.js?467f:17)
    at XMLHttpRequest.handleLoad (xhr.js?b50d:62)

我试图捕捉错误,但我无法获得任何有用的信息,说明它来自哪里以及如何修复它。 我认为这可能是导致问题的id prop ,但不确定。 ArticleShow组件的父级中,我在路由器链接中显示ArticleShow时定义了id prop属性。

ArticleCard.vue
<router-link
      class="article-link"
      :to="{ name: 'ArticleShow', params: { id: article.id } }"
      :article="this.article"
    >

然后再次在Articlecard.vue的父级中(前一个组件)

Home.vue
<ArticleCard
      v-for="article in articles"   // I imported *articles* from the vuex store
      :key="article.id"
      :article="article"
      class="ArticleCard"
></ArticleCard>

仍然不确定id prop是否真的是问题的根源。 无论如何,如果你能解决这个问题,请提供帮助,因为它已经持续了很长时间,非常感谢任何帮助。

错误 404 表示此 URL 不存在: http://localhost:5000/articles/undefined

所以这意味着你没有将任何id道具传递给ArticleShow组件

暂无
暂无

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

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