如果 xhr 请求被取消,则停止轨道控制器方法执行?Rails API



我正在尝试创建一个带有反应前端和rails API的SPA,我设法从javascript端停止了请求,考虑到这一点,我注意到即使我取消请求,rails服务器仍然执行控制器内部的内容。

def index
@dashboards = Dashboard.all
# Rest of controller logic, like calls to an external API
render json: json_response(@dashboards)
end

有没有办法捕获 xhr 请求被取消,以便我可以做这样的事情:

def index
@dashboards = Dashboard.all
if request.cancelled? throw :abort 
# It should not matter since request was cancelled in the frontend
render json: json_response(@dashboards)
end

不,抱歉,即使使用像 websockets 这样的有状态连接,也无法可靠地检测该连接何时断开。

最新更新