axios.get() is combining the url when sending requests to the backend.

纵然是瞬间 提交于 2020-12-13 07:52:08

问题


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

"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
      });
   });
}

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


回答1:


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

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



来源:https://stackoverflow.com/questions/52525634/axios-get-is-combining-the-url-when-sending-requests-to-the-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!