最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does Axios get call return a 404 error with basic setup? - Stack Overflow

programmeradmin4浏览0评论

I'm trying to set up a React project and want to get a basic get call working. The url I'm using is just for testing and to see if everything is working well. However, I get a 404 error back. The call I make is

axios.get('')
  .then(response => console.log(response))
  .catch(error => console.log(error.response));
<script src=".18.0/axios.js"></script>

I'm trying to set up a React project and want to get a basic get call working. The url I'm using is https://jsonplaceholder.typicode./todos/1 just for testing and to see if everything is working well. However, I get a 404 error back. The call I make is

axios.get('https://jsonplaceholder.typicode./todos/1')
  .then(response => console.log(response))
  .catch(error => console.log(error.response));
<script src="https://cdnjs.cloudflare./ajax/libs/axios/0.18.0/axios.js"></script>

When I try this call with the Fetch API, I get the correct response but with Axios, I get back this error {status: 404, config: {…}, data: undefined}. I've been trying to fix this for days and nothing has worked. Is there some type of obvious setup I may have missed?

I followed this tutorial for the most part: https://hackernoon./tutorial-how-to-make-http-requests-in-react-part-3-daa6b31b66be

Any help is appreciated :)

Share Improve this question edited Oct 30, 2018 at 15:19 Quentin 945k133 gold badges1.3k silver badges1.4k bronze badges asked Oct 30, 2018 at 15:16 anon311anon311 111 silver badge5 bronze badges 9
  • I can't reproduce the problem. – Quentin Commented Oct 30, 2018 at 15:19
  • Could you check in console or in networks tab what is the URL being called? – Aditya Commented Oct 30, 2018 at 15:20
  • I am just wondering if in base/global config the base url is already applied – Aditya Commented Oct 30, 2018 at 15:21
  • @Aditya Just checked, the url being called is the right one. I'm not sure what could be causing this. – anon311 Commented Oct 30, 2018 at 15:24
  • Can you print the plete request headers? The source and not the parsed – Aditya Commented Oct 30, 2018 at 15:26
 |  Show 4 more ments

1 Answer 1

Reset to default 5

I just had the same problem, as pointed out by @skyboyer the problem is likely to be due to having turned on the axios-mock-adapter. If this is the case, you can confirm by looking to your actual network requests in the browser console that axios is not doing any request at all (no request to the desired url appears in the list). Check in your code where is a line like this:

const mock = new MockAdapter(axios);

ment it (and related), and hopefully your problem will be solved.

It is also possible to create a mock adapter instance based on empty axios instance (i.e. empty base url). Something like this:

const mock = new MockAdapter(axios.create()); 

In this way you won't have to change your test environment each time you want to run test.

发布评论

评论列表(0)

  1. 暂无评论