提问人:AlexGherasim 提问时间:11/10/2022 更新时间:11/10/2022 访问量:866
SyntaxError:意外的令牌“<”、“<!DOCTYPE“...获取数据时 JSON 无效
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON when Fetching Data
问:
我正在尝试使用 Redux Thunk 为我的项目获取一些数据,但我收到屏幕截图中出现的错误。这是错误](https://i.stack.imgur.com/6Dqtm.png)
我的 thunk 函数 `
export const fetchCategoriesStart = () => createAction(CATEGORIES_ACTION_TYPES.FETCH_CATEGORIES_START);
export const fetchCategoriesSuccess = (categoriesArray) => createAction(CATEGORIES_ACTION_TYPES.FETCH_CATEGORIES_SUCCESS, categoriesArray);
export const fetchCategoriesFailed = (error) => createAction(CATEGORIES_ACTION_TYPES.FETCH_CATEGORIES_FAILED, error);
export const fetchCategoriesAsync = () => async (dispatch) => {
dispatch(fetchCategoriesStart())
try {
const response = await fetch('../../menu.json');
const categoriesArray = await response.json()
dispatch(fetchCategoriesSuccess(categoriesArray));
} catch (error){
dispatch(fetchCategoriesFailed(error));
}
}
`
我调度 thunk 函数的组件: `
import { fetchCategoriesAsync } from "../../store/menu/menu.action";
import { useDispatch } from "react-redux";
import { useEffect } from "react";
const Menu = () => {
const dispatch = useDispatch()
useEffect(() => {
dispatch(fetchCategoriesAsync());
}, [])
}`
我尝试使用 Redux Thunk 从 json 文件中获取一些产品,我希望有效负载是产品的数组
答: 暂无答案
评论