3

I have a turbo:submit-end event. I want to get the response body when the event fires. Assuming it's possible, how can I do this?

Jason Swett
  • 43,526
  • 67
  • 220
  • 351

1 Answers1

2

You can do this to get the response in the js.

lets say you have this form which triggers the turbo:submit-end

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

now to get the response, you have to do something like this:

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)
    })
  }