ruby-on-rails 如何从turbo:submit-end事件中获取响应正文?

uqdfh47h  于 2022-11-19  发布在  Ruby
关注(0)|答案(1)|浏览(129)

我有一个turbo:submit-end事件。我想在事件触发时得到响应体。假设这是可能的,我该怎么做呢?

eivnm1vs

eivnm1vs1#

您可以这样做以获得js中的响应。
假设你有一个触发加速的表单: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)
    })
  }

相关问题