本文介绍了当向后端发送请求时,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

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

"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

推荐答案

在网址中添加一个斜杠.否则,这是相对路径,就会发生.

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.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 20:14