【问题标题】:how to fix this axios call如何修复此 axios 调用
【发布时间】:2020-04-14 00:08:24
【问题描述】:

我更改了代码以在一个地方处理 API 错误,但它停止工作,任何人都可以找出问题所在

改变之前(工作正常)

action.js

export const login = (email, password) => dispatch => {
  axios
    .post('http://localhost:8000/v1/users/signin/', {
      email: email,
      password: password,
    })
    .then(res => {
      dispatch({
        type: LOGIN_USER,
        payload: res.data,
      });
    })
    .catch(err => console.log(err));
};

更改我的代码后

action.js

import { postRequest } from '../services';

export const login = (email, password) => dispatch => {
  postRequest('users/signin/', {
    email: email,
    password: password,
  })
    .then(res => {
      dispatch({
        type: LOGIN_USER,
        payload: res.data,
      });
    })
    .catch(err => console.log(err));
};

services.js

export const API_URL = 'localhost:8000/v1/';

export const postRequest = (request, body) => {
   return axios.post(API_URL + request, body);
};

【问题讨论】:

    标签: reactjs redux thunk


    【解决方案1】:

    您是否忘记了 API_URL 上的“http:”?

    export const API_URL = 'http://localhost:8000/v1/';

    【讨论】:

      猜你喜欢
      • 2019-10-10
      • 2022-09-30
      • 2013-05-19
      • 1970-01-01
      • 2020-07-18
      • 2022-01-11
      • 2010-12-22
      • 2011-12-30
      • 2021-01-29
      相关资源
      最近更新 更多