reactjs - Axios 向重复的 url 发送帖子

标签 reactjs http axios

我想做的是向这个本地 IP 地址发送请求,但它不断返回错误 404。我查看浏览器控制台,发现它重复了 url。

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

enter image description here

最佳答案

我成功解决了这个问题。只需添加 http://10.0.0.10

And then install Axios

安装 CORS:

npm i cors


const cors = require ('cors')
app.use (cors ()) (put at the beginning)

关于reactjs - Axios 向重复的 url 发送帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65639496/

相关文章:

javascript - Axios请求在iOS上显示响应,但不适用于android

javascript - Webback list 文件未编译为 es5

javascript - 如何使用 React Native 在 Android 上使用与 ios 相同的自定义字体

javascript - 将 redux-persist 与 redux-immutable 一起使用时出错

Angular HttpClient 帖子不起作用

javascript - 当有 OnGoing Request 时如何处理连接切换?

javascript - 使用 Bootstrap 在 React 中显示行

angular - 启动 Angular 应用程序时如何获取第一个 http 请求的 header (http ://localhost:4200/)

javascript - 确实需要一些帮助来在渲染之前弄清楚 componentWillMount() 的逻辑

node.js - Nuxt Express 中间件获取请求中未定义 req.query