在TypeScript create-react-app项目上使用Axios GET请求时,我遇到了一个奇怪的问题。
当前,我正在后端运行TypeScript Express应用,并且有一条返回对象的简单路由:
router.get('/api/current_user', (req: Request, res: Response) => {
res.send(req.user);
});
当我通过URL访问此路线时,我可以很好地看到该对象。这是一个像这样的JSON对象:
{
"_id": "randomstring",
"googleId": "randomstring",
"username": "Alexander Le",
"__v": 0
}
到目前为止完全正常。大。 但是,嗯。
我对此路由发出Axios GET请求时,总是收到一个空字符串作为数据。这是我的axios请求:
export const fetchUser = () => async (dispatch: Dispatch) => {
const response = await axios.get('http://localhost:5000/api/current_user');
dispatch({
type: FETCH_USER,
payload: response
});
};
我检查了我的网络标签,尽管我总是得到200的状态,但我总是收到一个空字符串作为数据。
要添加到此内容,当我从此更改路线响应时:
router.get('/api/current_user', (req: Request, res: Response) => {
res.send(req.user);
});
为此:
router.get('/api/current_user', (req: Request, res: Response) => {
res.send('HELLO');
});
我得到“ HELLO”作为数据。那么是什么阻止了我将JSON对象作为数据接收?
提前谢谢!