hauleth / vanilla-ujs

UJS without jQuery dependency
MIT License
194 stars 24 forks source link

Do not send FormData when it is empty #29

Closed martijnversluis closed 7 years ago

martijnversluis commented 7 years ago

Fix for hauleth/vanilla-ujs #28. For the full bug report please refer to https://github.com/hauleth/vanilla-ujs/issues/28.

Basically, when creating a FormData object from a form that has no inputs to be sent (inputs that are disabled, inputs without name and unchecked checkboxes/radio buttons are generally not included in a form request) and sending that as the body for a XMLHttpRequest, it will result in a corrupt multipart request. Unfortunately there is poor support for iterating a FormData object to find out if it is empty (at least it did not work in PhantomJS).

This fix tries to predict if the FormData object will be empty by inspecting the form elements. If it can find no form input that will be included in the request, the data is nullified before passing it to LiteAjax.ajax().

hauleth commented 7 years ago

LGTM

Close #28

jankeesvw commented 7 years ago

@hauleth could you please release this to RubyGems, we are still using the git master branch in our project.