How is the MIME boundary set with HTML5 FormData and Ajax.Request()?

Skip to first unread message

Adam Nielsen

Jul 19, 2016, 12:51:55 AM7/19/16
to Prototype &
Hi all,

I am struggling a bit to get a HTML5 FormData object submitting with Prototype's Ajax.Request().

Because the form data needs to be submitted as multipart/form-data, I need to set the Content-Type to include the MIME boundary, otherwise the receiving end can't decode the multipart/form-data content.

I am doing something like this:

var d = new FormData();
.append('name', $F('name'));
.append('type', 'blah');
.append('filecontent', $('file').files[0]);

new Ajax.Request('url', {
: 'post',
//contentType: 'multipart/form-data', // need MIME boundary
: d,
: function(r) { }

If I leave out the contentType parameter, then the form gets submitted as application/x-www-form-urlencoded, but the content is in multipart/form-data so it is not possible to decode it.  If I set the contentType to multipart/form-data then the recipient complains that the MIME boundary is missing from the HTTP Content-Type header.

How can I get the contentType set to include the MIME boundary used by the HTML5 FormData object?

Many thanks,

Adam Nielsen

Jul 19, 2016, 1:14:22 AM7/19/16
to Prototype &
How can I get the contentType set to include the MIME boundary used by the HTML5 FormData object?

I've done a little more investigating, and if I make this change to prototype.js then it fixes the problem:

diff --git a/js/prototype.js b/js/prototype.js
index cc89daf
..70e2e58 100644
--- a/js/prototype.js
+++ b/js/prototype.js
@@ -1762,8 +1762,6 @@ Ajax.Request = Class.create(Ajax.Base, {
if (this.method == 'post') {
-      headers['Content-type'] = this.options.contentType +
-        (this.options.encoding ? '; charset=' + this.options.encoding : '');
/* Force "Connection: close" for older Mozilla browsers to work
        * around a bug where XMLHttpRequest sends an incorrect

Is there any way to tell Prototype to leave the HTTP Content-Type unset?  It looks like jQuery handles this by having contentType set to false, but I can't see how Prototype handles it.

Reply all
Reply to author
0 new messages