在 React 中发送 Fetch 删除请求到 Rails API 的问题
Issues sending Fetch delete request in React to Rails API
我能够很好地发送 get 和 put 方法,但令人惊讶的是我无法将删除提取请求从我的 Redux 操作发送到我的 Rails 后端。这更加令人困惑,因为在 Postman 中我能够很好地命中 Destroy 路线。我到处搜索修复程序,但没有找到任何有效的方法。我有一个触发发送此请求的 Redux 操作的 onClick 函数:
export const deleteQuestion = (questionId, routerHistory) => {
return dispatch => {
return fetch(`${API_URL}/questions/${questionId}`, {
method: 'DELETE',
}).then(response => {
dispatch(removeQuestion(questionId));
routerHistory.replace(`/`);
})
.catch(error => console.log(error));
};
};
我检查了很多次以确保语法和路由没有问题。 questionId 也是正确的问题 ID。但是,无论我做什么,Questions 控制器中的 Destroy 方法都不会识别该请求。我检查了 Rails 中的路线,它存在。我没有收到任何错误,没有请求发送到服务器,也没有返回任何内容,终端、控制台或任何地方都没有。
这是 Github 帐户:https://github.com/jwolfe890/react_project1
我真的很感激任何人的任何见解。谢谢!
您的 deleteQuestion
方法 return 是一个带有 dispatch
参数的匿名函数,似乎从未被调用 (Calling code)。仅调用 deleteQuestion
但不调用由它编辑的函数 return。
因为它是由点击处理程序调用的,所以我会说您实际上想要这样的东西:
export const deleteQuestion = (questionId, routerHistory) => {
fetch(`${API_URL}/questions/${questionId}`, {
method: 'DELETE',
}).then(response => {
dispatch(removeQuestion(questionId));
routerHistory.replace(`/`);
})
.catch(error => console.log(error));
};
或者,如果您想 return 承诺,您当然可以将其更改为:
export const deleteQuestion = (questionId, routerHistory) => {
return fetch(`${API_URL}/questions/${questionId}`, {
method: 'DELETE',
}).then(response => {
dispatch(removeQuestion(questionId));
routerHistory.replace(`/`);
})
.catch(error => console.log(error));
};
如果你想动态注入 dispatch 函数,你可以保留原来的代码,但必须这样调用方法:
deleteQuestion(this.state.question.id, history)(myDispatchMethod);
我能够很好地发送 get 和 put 方法,但令人惊讶的是我无法将删除提取请求从我的 Redux 操作发送到我的 Rails 后端。这更加令人困惑,因为在 Postman 中我能够很好地命中 Destroy 路线。我到处搜索修复程序,但没有找到任何有效的方法。我有一个触发发送此请求的 Redux 操作的 onClick 函数:
export const deleteQuestion = (questionId, routerHistory) => {
return dispatch => {
return fetch(`${API_URL}/questions/${questionId}`, {
method: 'DELETE',
}).then(response => {
dispatch(removeQuestion(questionId));
routerHistory.replace(`/`);
})
.catch(error => console.log(error));
};
};
我检查了很多次以确保语法和路由没有问题。 questionId 也是正确的问题 ID。但是,无论我做什么,Questions 控制器中的 Destroy 方法都不会识别该请求。我检查了 Rails 中的路线,它存在。我没有收到任何错误,没有请求发送到服务器,也没有返回任何内容,终端、控制台或任何地方都没有。
这是 Github 帐户:https://github.com/jwolfe890/react_project1
我真的很感激任何人的任何见解。谢谢!
您的 deleteQuestion
方法 return 是一个带有 dispatch
参数的匿名函数,似乎从未被调用 (Calling code)。仅调用 deleteQuestion
但不调用由它编辑的函数 return。
因为它是由点击处理程序调用的,所以我会说您实际上想要这样的东西:
export const deleteQuestion = (questionId, routerHistory) => {
fetch(`${API_URL}/questions/${questionId}`, {
method: 'DELETE',
}).then(response => {
dispatch(removeQuestion(questionId));
routerHistory.replace(`/`);
})
.catch(error => console.log(error));
};
或者,如果您想 return 承诺,您当然可以将其更改为:
export const deleteQuestion = (questionId, routerHistory) => {
return fetch(`${API_URL}/questions/${questionId}`, {
method: 'DELETE',
}).then(response => {
dispatch(removeQuestion(questionId));
routerHistory.replace(`/`);
})
.catch(error => console.log(error));
};
如果你想动态注入 dispatch 函数,你可以保留原来的代码,但必须这样调用方法:
deleteQuestion(this.state.question.id, history)(myDispatchMethod);