大家好,我不知道是否有人可以帮助在这一点上,但当使用createAsyncThunk和Axios每当我试图张贴的东西,我得到一个400错误。我知道这意味着这是一个糟糕的要求,但我不知道为什么我会得到它。这里是我的代码
const API_URL =
'https://us-central1-bookstore-api-e63c8.cloudfunctions.net/bookstoreApi/apps/sppWoQdq6XBTog313fKt/books';
export const defaultState = [];
export const getBooks = createAsyncThunk(GET_BOOKS, async () => {
const response = await axios.get(API_URL);
return response.data;
});
export const addBook = createAsyncThunk(ADD_BOOK, async (book) => {
const response = await axios.post(API_URL, book);
return response.data;
});
export const removeBook = createAsyncThunk(REMOVE_BOOK, async (book) => {
const response = await axios.delete(`${API_URL}${book.id}`);
return response.data;
});
export const bookSlice = createSlice({
name: 'books',
initialState: defaultState,
extraReducers: (builders) => {
builders.addCase(getBooks.fulfilled, (action) => action.payload);
},
});
export default bookSlice.reducer;
我已经尝试了一切,我可以理解为什么我得到这个错误不同的api的不同代码,我不知道问题是什么
你可以尝试强制json header:
axios.post(API_URL, book, {headers: {'Content-Type': 'application/json'}})