Axios baseURL 未附加react.js 中的URL

Hem*_*amy 2 javascript reactjs react-redux axios

我已经使用 saga.js 进行了 axios 设置,当用户单击“注册”按钮时必须分派一个操作并且它将满足请求,一切正常,除了在 url 中附加 baseURL , url http://users/register

我的 axios 设置,

 import axios from 'axios';

const axiosClient = axios.create({
    baseURL : `https://test-url.com/`
});


export function postRequest(URL, payload) {
    return axiosClient.post(`/${URL}`, payload).then(response => response);
}
Run Code Online (Sandbox Code Playgroud)

传奇.js

import { put, takeEvery, all, call } from 'redux-saga/effects';
import {register} from "./serviceWorker";
import { postRequest } from './helpers/axiosClient';
import actions from "./redux/actions";

function* registerUser(params) {
    try {
        const response = yield call(() => postRequest('/users/register', params.payload));
        console.log('response', response.data);
        yield put({
            type: actions.ON_REGISTER_SUCCESS,
            payload: response.data,
        });
    } catch (error) {}
}

function* watchIncrementAsync() {
    yield takeEvery('ON_REGISTER', registerUser)
}

export default function* rootSaga() {
    yield all([
        watchIncrementAsync()
    ])
}
Run Code Online (Sandbox Code Playgroud)

Aru*_*han 7

如果删除添加到URLin 的前导斜杠postRequest,axios 将使用基本 URL 来构建完整路径。

return axiosClient.post(URL, payload).then(response => response);
Run Code Online (Sandbox Code Playgroud)

如果您想了解发生了什么,这就是 axios构建完整路径的地方。它认为您正在传递绝对 URL(因为路径'//users/register'以双斜杠开头)并且不使用基本 URL。