我目前正在开发一个 React-Rails 应用程序,并试图弄清楚如何从 success 函数将已删除的记录传递到父组件以更新父组件的 View 。
这是有问题的代码:
handleDelete (e) {
var that = this;
var url = "/records/" + this.props.data.id;
e.preventDefault();
$.ajax({
method: 'DELETE',
url: url,
dataType: 'JSON',
success: (data) => {
console.log(data);
that.props.handleDeleteRecord(data);
}
});
}
上面的console.log(data)返回未定义。
我的问题是 Ajax 似乎没有向 success 函数传递任何内容。真的吗?
最佳答案
您需要编辑 Rails RecordsController
以便它在请求后呈现某种类型的数据。我推荐 JSON。
def destroy
record = Record.find(params[:id])
record.destroy
render json: record
end
这样,您将把刚刚删除的记录的 JSON 形式传回 AJAX 调用的成功函数。
关于javascript - Ajax 删除请求中传递给 success 函数的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37355598/