我正在尝试使用fetch delete request方法使用react redux删除我的localhost服务器中的项目
调用方法
deleteItem(e) {
e.preventDefault();
const id = this.props.id;
this.props.deleteSet(id);
}
调度行动
const mapDispatchToProps = dispatch => ({
deleteSet: id => dispatch(deleteSet(id)),
});
动作
export function deleteSetSuccess(id) {
return {
type: "DELETE_SET_SUCCESS",
id,
};
}
export function deleteSet(data) {
return (dispatch) => {
fetch(`${apiUrl}orgs/1/sets/${data}`, {
method: "DELETE",
body: JSON.stringify(data),
headers: new Headers({
"Content-Type": "application/json",
}),
}).then(response => response)
.then(id => dispatch(deleteSetSuccess(id)));
};
}
减速
export function deleteSetSuccess(state = '', action) {
switch (action.type) {
case "DELETE_SET_SUCCESS":
return action.id;
default:
return state;
}
}
来自localhost服务器的响应
DELETE http://localhost:8080/distinction-2.0-alpha2/api/orgs/1/sets/8 400 (Bad Request)
答案 0 :(得分:1)
valid HTTP DELETE
request doesn’t have a request body - 所以也不需要Content-Type
请求标头 - 但问题中的请求代码是将一些数据作为请求正文发送,还有Content-Type
请求标头。据推测,您的服务器正在决定这不是有效的DELETE
请求,因此它正在响应400“错误请求”来表示。