【问题标题】:How to get response body from turbo:submit-end event?如何从 turbo:submit-end 事件中获取响应正文?
【发布时间】:2022-11-08 20:11:38
【问题描述】:

我有一个turbo:submit-end 事件。我想在事件触发时获取响应正文。假设有可能,我该怎么做?

【问题讨论】:

    标签: ruby-on-rails hotwire-rails


    【解决方案1】:

    您可以这样做以获取 js 中的响应。

    假设您有这种触发 turbo:submit-end 的表单

    <%= form_with(url: some_path, data: {  action: 'turbo:submit-end->some-js-controller#someActionMethod' }) do |f| %>
    

    现在要获得响应,您必须执行以下操作:

    someActionMethod(event) {
        event.detail.fetchResponse.response.text().then(value => {
          //value contains the response, if response is html, if response is 
          //json, event.detail.fetchResponse.response.json() (did not try, 
          //should work)
        })
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多