【发布时间】:2022-11-08 20:11:38
【问题描述】:
我有一个turbo:submit-end 事件。我想在事件触发时获取响应正文。假设有可能,我该怎么做?
【问题讨论】:
标签: ruby-on-rails hotwire-rails
我有一个turbo:submit-end 事件。我想在事件触发时获取响应正文。假设有可能,我该怎么做?
【问题讨论】:
标签: ruby-on-rails hotwire-rails
您可以这样做以获取 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)
})
}
【讨论】: