axios.get() 在向后端发送请求时组合 url。

Wol*_*Tru 1 react-redux axios

我通过 axios 向后端发送请求,我的地址栏上的 URL 是“localhost:3000/topics/5ba06e74dbc”

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

"localhost:3000/topics/api/topics/5ba06e74dbc 404 (Not Found)" 请求应该是: "localhost:3000/api/topics/5ba06e74dbc" 任何人都知道为什么在前面添加了额外的 "topics/" api调用?

// 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
      });
   });
}
Run Code Online (Sandbox Code Playgroud)

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

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

cfr*_*ser 7

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

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