weird problem or bug. I’m using the jQuery Form Plugin and it works fine everywhere accept on one form where I have a single file-upload with enctype:multipart/form-data on the form. On this form I’m facing two weird things …
- the JSON object that is returned from the server is empty!
- In Opera the Submit-button even triggers a file-download!
However this occurs only if I leave the enctype:multipart/form-data and the input type="file" in the form. Without it everything works fine and the JSON Object is returned correctly – and there is no download in Opera.
HTML:
<form accept-charset="UTF-8" action="/ajax/profiledetails" id="profileAboutMeForm" method="post" novalidate="novalidate" encoding="multipart/form-data" enctype="multipart/form-data">
...
<p class="rel avatarUpload">
<label class="label" for="profileAvatar">Choose Avatar</label>
<img class="profileAvatar avatar30" src="" alt="user">
<input class="fileUpload br3" id="profileAvatar" name="profile[avatar]" type="file">
</p>
...
</form>
jQuery:
$(formId).ajaxSubmit({
type: "POST",
cache: false,
resetForm: reset,
dataType: "text json",
success: function(jsonObject, status) {
console.log("status + ", jsonObject.status: "+ jsonObject.status + ", jsonObject.data: " + jsonObject.data);
Any idea what could cause that? How I could fix that?
Thank’s in advance.
edit:
What I never tried though was to just log the object itself and here it turns out that in this case (only if the file-input and enctype is set) the jsonObject is a STRING and not an object.
if (typeof jsonObject == 'string')
console.log('yes, it's a string'); //yes, it's a string
jsonObject = JSON.parse(jsonObject);
console.log(jsonObject);
So, this means I have a JSObject again in my javascript and this fixes my first problem, however the opera-bug still remains! Any ideas?
Just as a starting point, I assume you’ve read the documentation about this on the plugins page at http://jquery.malsup.com/form/#file-upload? You won’t get access to the JSON within ajaxSubmit() because the response is actually written to a hidden iframe used for the upload.
Here is the code he uses on the examples page:
The
successmethod is what matters for you here…notice that he is writing the returndatato the page for debugging purposes.