Javascript 使用post请求来更新数据库的React/redux
我正在尝试使用react/redux更新我的数据库,但不知如何更新。据我所知,可以使用fetchpost请求来完成。我试图在我的动作创建者中做到这一点,但我认为我对post请求的语法缺乏全面的理解Javascript 使用post请求来更新数据库的React/redux,javascript,reactjs,redux,Javascript,Reactjs,Redux,我正在尝试使用react/redux更新我的数据库,但不知如何更新。据我所知,可以使用fetchpost请求来完成。我试图在我的动作创建者中做到这一点,但我认为我对post请求的语法缺乏全面的理解 如果我能得到一个使用fetch来使用post请求的正确方法的例子,我将不胜感激,甚至是一个指向正确方向的链接 我从Mozilla中获取示例: 然后,在代码中,您可以在需要执行操作时使用postData,并在还原程序中侦听“POST_SOMETHING”操作 export function postDa
如果我能得到一个使用fetch来使用post请求的正确方法的例子,我将不胜感激,甚至是一个指向正确方向的链接 我从Mozilla中获取示例: 然后,在代码中,您可以在需要执行操作时使用postData,并在还原程序中侦听“POST_SOMETHING”操作
export function postDataSuccess(response) {
return {
type: 'POST_SOMETHING',
response
};
}
export function postData(url, data) {
return dispatch => {
fetch(url, {
body: JSON.stringify(data), // must match 'Content-Type' header
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, same-origin, *omit
headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
},
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, cors, *same-origin
redirect: 'follow', // manual, *follow, error
referrer: 'no-referrer', // *client, no-referrer
})
.then(response => dispatch( postDataSuccess(response.json()) ));
}
}