Tuesday 30 October 2012

IE problems with jQuery.Form AJAX POSTs

jQuery.Form is an excellent plugin for jQuery that makes it really easy to post HTML forms via AJAX and parse the resulting response in a JavaScript handler (similar to jQuery's standard AJAX methods).

However, when submitting a "multipart/form-data"-form with a JSON response (content type set to "application/json"), Internet Explorer tries to download the response instead of calling your JavaScript reponse handler...  very irritating indeed. Of course, Firefox and Chrome handle these responses without any issues (I haven't tried IE10 yet).

A workaround is to set the response type to "text/html" on the server, and parse the resulting text string via jQuery.parse(). Not ideal, but at least works on all browsers that I tested.

On a side note: if you set the response content type to "text/plain", IE (and some versions of Firefox) will add <pre> tags to the response string - thus using "text/html" instead is quicker.