我正在使用 axios 并收到 400 个错误的请求错误。我正在使用 react-redux 并尝试向 localhost:3000/posts 发送 post 请求。这是我正在使用的代码。
import axios from 'axios';
import {
GET_ALL_POSTS,
GET_POST,
CREATE_POST,
DELETE_POST,
UPDATE_POST
} from './types';
const ROOT_URL = 'http://localhost:3000';
export function createPost({content, title}, cb) {
return function(dispatch) {
axios.post(`${ROOT_URL}/posts`, {content, title})
.then((response) => {
console.log(response);
dispatch({
type: CREATE_POST,
payload: response
});
})
.then(() => cb())
.catch((error) => {
console.log("Problem submitting New Post", error);
});
}
}
Run Code Online (Sandbox Code Playgroud)
我也收到此错误,问题不在于服务器、axios 或代理 url。问题是我没有从我的反应应用程序发送正确的数据。例如我应该发送:
{ email: 'ib2@gmail.com', password: 'asdf' }
我发送的是:
{ name: 'ib2@gmail.com', password: 'asdf' }
这导致 api 不理解名称字段,因为我提供了电子邮件作为 api 中的密钥。
因此,如果您仍然遇到问题,请尝试检查您是否发送了正确的数据。