简体   繁体   English

当我使用 createAsyncThunk 和 axios 时,我不断收到 400 错误

[英]I keep getting a 400 error when i am using createAsyncThunk and axios

Hi everyone i dont know if anyone can help on this but when using createAsyncThunk and Axios whenever i try to POST something I get a 400 error.大家好,我不知道是否有人可以提供帮助,但是当我尝试发布某些内容时使用 createAsyncThunk 和 Axios 时,我会收到 400 错误。 And I know that means it is a bad request but I dont know why I am getting it.我知道这意味着这是一个糟糕的请求,但我不知道为什么我会收到它。 And here my code这是我的代码

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;

I have tried everything i can to understand why I am getting this error differant api's differant code i dont know what the problem is我已经尽我所能来理解为什么我会收到这个错误不同的 api 的不同代码我不知道问题是什么

You can try to force json header:可以试试强制json header:

axios.post(API_URL, book, {headers: {'Content-Type': 'application/json'}})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM