我正在使用 axios 并收到 400 bad request 错误。我正在使用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);
});
}
}
最佳答案
我也遇到了这个错误,问题不在于服务器,也不在于 axios 或代理 url。
问题是我没有从我的 react 应用程序发送正确的数据。
例如
我应该发送:
{ 电子邮件:'ib2@gmail.com',密码:'asdf' }
我发送的是:
{ 名称:'ib2@gmail.com',密码:'asdf' }
这导致 api 无法理解名称字段,因为我提供了电子邮件作为 api 中的 key 。
因此,如果您仍然遇到问题,请尝试检查您发送的数据是否正确。
关于reactjs - 使用 axios 收到 400 错误错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731340/