[英]GET request returns Error 404 through Axios, but works in Postman?
[英]Axios GET Request works in Postman but throws 400 error - Redux
我正在完成一個完整的堆棧項目。 到目前為止,我已經創建了一個登錄和注冊,它們都可以工作並且它們返回一個 jwt Web 令牌。 我正在嘗試向 (/api/profile/me) 發出 GET 請求以獲取登錄的用戶配置文件,但出現 400 錯誤。 它在 Postman 中有效,但沒有反應。
動作 - profile.js
import axios from 'axios';
import { GET_PROFILE, PROFILE_ERROR } from './types';
// Get current users profile
export const getCurrentProfile = () => {
return async (dispatch) => {
try {
const res = await axios.get('/api/profile/me');
dispatch({
type: GET_PROFILE,
payload: res.data,
});
} catch (error) {
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
};
減速器 - profile.js
import { PROFILE_ERROR, GET_PROFILE } from '../actions/types';
const initialState = {
profile: null,
profiles: [],
repos: [],
loading: true,
error: {},
};
export default function (state = initialState, action) {
const { type, payload } = action;
switch (type) {
case GET_PROFILE:
return {
...state,
profile: payload,
loading: false,
};
case PROFILE_ERROR:
return {
...state,
error: payload,
loading: false,
};
default:
return state;
}
}
我要加載操作的儀表板
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { useSelector, useDispatch } from 'react-redux';
import { getCurrentProfile } from '../../actions/profile';
const Dashboard = () => {
const auth = useSelector((state) => state.auth);
const profile = useSelector((state) => state.profile);
const dispatch = useDispatch();
useEffect(() => {
dispatch(getCurrentProfile());
}, []);
return (
<div className="container">
<p className="dashboard-heading">Dashboard</p>
</div>
);
};
export default Dashboard;
我通過控制台記錄錯誤響應數據發現了這個問題。 它返回了消息
“沒有該用戶的個人資料”
這意味着我使用的用戶與我在 Postman 中使用的用戶不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.