Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get response body from turbo:submit-end event?

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?

like image 737
Jason Swett Avatar asked Oct 27 '25 08:10

Jason Swett


1 Answers

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)
    })
  }
like image 153
Kinley Wangchuk Avatar answered Oct 28 '25 22:10

Kinley Wangchuk