我正在使用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);
});
}
}
{content, title}
。也许首先值得尝试硬编码请求为类似{content:"hello",title:"world"}
,以查看是否修改了400错误?或者可能只使用一个元素axios.post(url,content).then...
试试。 - GregHNZ