mikehostetler / amplify

AmplifyJS
http://amplifyjs.com
GNU General Public License v2.0
1.44k stars 144 forks source link

amplify.js incompatible with FormData #107

Open devconcept opened 9 years ago

devconcept commented 9 years ago

In a recent work I had to write an application that send files using ajax. In jQuery the syntax was

$.ajax({
   url: formURL,
   type: 'POST',
   data: formData,
   contentType: false,
   cache: false,
   processData: false,
   success: ...,
   error: ...
});

On amplify.js it just did'n work. No matter what the options were. After some debbuging I found that the FormData object passed was turned to a regular javascript object after the ajax preprocess message was executed and then passed to jQuery. The library already added an exception in case the object passed were a string so I just add another exception so the object were ignored if was a native FormData object.

amplify.subscribe( "request.ajax.preprocess", function(defnSettings, settings, ajaxSettings) {
        var mappedKeys = [],
            data = ajaxSettings.data;

        if (typeof data === "string" || Object.prototype.toString.call(data) === "[object FormData]") {
            return;
        }
......(omited)
)}

Maybe you can check for constructor or perform some other work so I share the solution as well so you can improve it.