I
I
Ivan2010-12-07 16:54:46
JavaScript
Ivan, 2010-12-07 16:54:46

jQuery and Form Plugin: File Upload?

So the problem is:

I'm submitting a form to the server using the ajaxSubmit() method of the above plugin. Forms without a file element work just fine. The result is returned as a JSON string.

Problems arise when a file selection is added to the form. In the form template, I automatically get the corresponding enctype. The plugin sends everything, the result comes in the right form (looked through firebug). The first problem is that the browser offers to save the result as a file. Googling, I came to the conclusion that the Content-Type of the response had to be changed from 'application/json; charset=UTF-8'to default 'text/html; charset=utf-8'.

Now the response comes "to the studio", but the success function does not work. Apparently, now the gag is in the format of the expected result - after all, I specified the optiondataType: 'json'. Removed the option - success works, but, apparently, the plugin is trying to convert the result to html format, because tags like HEAD and BODY are added. Moreover, firebug shows that the initial result was correct, i.e. it's a bug of the plugin.

Has anyone encountered such problems? How to solve them? I use jQuery 1.4.2 (tried to switch to 1.3.2 - the same rake) and Form 2.5.1 (like the latest version).

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Pavel Chipak, 2010-12-07
@reket

Indeed ... I also had problems with uploading files, but I don’t remember how I decided, so I thought maybe it helped.

P
Pavel Chipak, 2010-12-07
@reket

Why is the ajaxForm method not suitable?

K
Konstantin Kitmanov, 2010-12-07
@k12th

This is because the only cross-browser method for uploading files with AJAX is to create an invisible /> and specify the target="upload" attribute on the form.
The plugin reads this response only as text.
Solution: parse JSON yourself.

N
nikitammf, 2010-12-08
@nikitammf

    As far as I remember the problem is in firebug. Try with firebug disabled. The thing is, as mentioned above, the form is sent to the iframe and the plugin takes document.body.innerHTML, and our favorite firebug adds a couple of its divs to the iframe - json is not valid.
    There is a way to return not just json, but json wrapped in “pre” via textarea or response.
    But it's all about firebug, that is, if a person inspects your site through firebug, then it will not work for him.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question