Api 将提取post请求转换为axios post请求
我正在尝试将以下Api 将提取post请求转换为axios post请求,api,axios,fetch,Api,Axios,Fetch,我正在尝试将以下fetch请求转换为axios,但失败 export const createPost = postData => dispatch => { fetch('https://jsonplaceholder.typicode.com/posts', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(
fetch
请求转换为axios
,但失败
export const createPost = postData => dispatch => {
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(postData)
})
.then(res => res.json())
.then(post =>
dispatch({
type: NEW_POST,
payload: post,
})
);
};
我就是这样做的
export const createPost = postData => dispatch => {
axios({
method: "post",
url: "https://jsonplaceholder.typicode.com/posts",
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(postData),
})
.then((res) => dispatch({
type: NEW_POST,
payload: res,
}))
.catch((err) => console.log(err));
}
这就是调用上述函数的方式
const post = {
title: this.state.title,
body: this.state.body
};
this.props.createPost(post);
你能帮我做这个吗