我正在使用 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。
问题是我没有从我的反应应用程序发送正确的数据。
例如
我应该发送:{ email: '[email protected]', password: 'asdf' }
我发送的是:{ name: '[email protected]', password: 'asdf' }
这导致 api 不理解名称字段,因为我提供了电子邮件作为 api 中的密钥。
因此,如果您仍然遇到问题,请尝试检查您是否发送了正确的数据。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)