首页 > 解决方案 > Axios 将帖子发送到重复的 url

问题描述

我想要做的是向这个本地 IP 地址发送一个请求,但它一直返回错误 404。我去查看浏览器控制台,它正在复制 url。

反应 + axios 代码:

import axios from "axios";
import { getToken } from "./auth";

const api = axios.create({
    baseURL: '10.0.0.10:3333'
  });

api.interceptors.request.use(async config => {
  const token = getToken();
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

export default api;

组件代码

try {
        const response = await api.post("/sessions", { email, password });
        login(response.data.token);
        this.props.history.push("/app");
      } catch (err) {
        this.setState({
          error:
            "Error"
        });

在此处输入图像描述

标签: reactjshttpaxios

解决方案


在基本网址之前添加前缀“http://”怎么样?


推荐阅读