axios.get() 在向后端发送请求时组合 url. [英] axios.get() is combining the url when sending requests to the backend.

查看:36
本文介绍了axios.get() 在向后端发送请求时组合 url.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 axios 调用向后端发送请求,地址栏上的 URL 是本地主机:3000/topics/5ba06e74dbc"

I'm sending a request to the backend via axios calls the URL on my address bar is "localhost:3000/topics/5ba06e74dbc"

但在我的浏览器检查器中返回错误

but in my browser inspector its returning an error

本地主机:3000/topics/api/topics/5ba06e74dbc 404(未找到)"请求应该是:本地主机:3000/api/topics/5ba06e74dbc"有谁知道为什么在 api 调用前添加额外的主题/"?

"localhost:3000/topics/api/topics/5ba06e74dbc 404 (Not Found)" the request should be: "localhost:3000/api/topics/5ba06e74dbc" anyone know why that extra "topics/" is being added in front of the api call?

// my action call I suspect it might be because of my routes or because 
//Im calling from topics/ already.
export const viewTopic = (id) => dispatch => {
  return axios.get(`api/topics/${id}`).then(res => {
    return dispatch({
          type: VIEW_TOPIC,
          payload: res.data
      });
   });
}

看看我的 GitHub 或询问更多信息,我不确定要包含什么.

take a peek at my GitHub or ask for more info, I'm not sure what to include.

https://github.com/wolffles/bloccit-node/tree/frontend

推荐答案

向 url 添加前导斜杠.否则,它是一个相对路径,就会发生这种情况.

Add a leading slash to the url. Otherwise, it's a relative path and that happens.

api/topics/${id} -> /api/topics/${id}

这篇关于axios.get() 在向后端发送请求时组合 url.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆