New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rails-ujs doesn't submit remote forms submitted with JavaScript via XHR #29546
Comments
@mrhead I've recently encountered this issue as well. By looking at the current rails-ujs codebase, seems like as a temporary replacement, we can let the rails-ujs handle the XHR form submission. Example usage (if using jQuery): nativeFormElement = $(this)[0]; //get the native form element
Rails.handleRemote.call(nativeFormElement, event); // throws error on callback after succesful request, because we don't have the proper 'submit' CustomEvent object as a target
// or ...
Rails.handleRemote.call(nativeFormElement, event);
// or ...
Rails.fire(nativeFormElement, 'submit'); // s̶e̶e̶m̶s̶ ̶t̶o̶ ̶c̶u̶r̶r̶e̶n̶t̶l̶y̶ ̶s̶u̶b̶m̶i̶t̶ ̶v̶i̶a̶ ̶X̶H̶R̶ ̶t̶w̶i̶c̶e̶ this works just fine. |
@mrhead I've given this much thought over the weekend and it turns out that As far as using Web API, It's better to use the more recent form = document.querySelector('form');
form.dispatchEvent(new Event('submit', {bubbles: true})); // you can specify more options in `Event()` for reliability across different browsers. Or use just use a convenient wrapper provided by form = document.querySelector('form');
Rails.fire(form, 'submit'); In light of this, I think this issue can be closed. |
@padi Whoa, nice investigation! 👏 |
i've extracted some minor documentation from this into the rails-ujs wiki https://github.com/rails/jquery-ujs/wiki/How-to-trigger-a-form-submit-from-code |
Arrived here after migrating from So, for the time being, we're downgrading back to It would be very nice if someone with better JS skills could debug why the previous library managed to get it right and the new one doesn't. |
I've found an inconsistency between jquery_ujs and rails-ujs. With jquery_ujs a remote form submitted with JavaScript is submitted via XHR, but with rails-ujs it is submitted via normal HTML request.
I am not sure if this is an intentional change or a bug.
Steps to reproduce
Edit
app/views/home/index.html.erb
and add:Start rails server:
rails s
Visit http://localhost:3000/home/index
Open developer console and run:
Expected behavior
Form is submitted via XHR request.
Actual behavior
Form is submitted via standard HTML request.
System configuration
Rails version: 5.1.1 and rails master
Ruby version: 2.4.1
The text was updated successfully, but these errors were encountered: