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

Stop rails controller method execution if xhr request is cancelled? Rails API

我正在尝试创建一个带有 React 前端和 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 这样的有状态连接,也无法可靠地检测到连接何时断开。