jQuery 表单插件:enctype:multipart/form-data 和文件上传 - 没有 JSON 返回?
奇怪的问题或错误。我正在使用 jQuery 表单插件,它在任何地方都可以正常工作,在我有一个表单的地方接受在表单上使用 enctype:multipart/form-data
进行文件上传。在此表单上,我面临两个奇怪的事情……
- 从服务器返回的 JSON 对象是空的!
- 在 Opera 中,提交按钮甚至会触发文件下载!
但是,只有当我在表单中保留 enctype:multipart/form-data
和 input type="file"
时,才会发生这种情况。没有它,一切都会正常工作,并且 JSON 对象会正确返回 - 并且 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);
知道什么可能导致这种情况吗?我该如何解决这个问题?
提前致谢。
编辑:
但我从未尝试过只是记录对象本身,事实证明,在这种情况下(仅当设置了文件输入和 enctype 时) jsonObject 是一个 STRING 而不是一个目的。
if (typeof jsonObject == 'string')
console.log('yes, it's a string'); //yes, it's a string
jsonObject = JSON.parse(jsonObject);
console.log(jsonObject);
所以,这意味着我的 javascript 中再次有了一个 JSObject,这解决了我的第一个问题,但是 opera-bug 仍然存在!有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为一个起点,我假设您已经阅读了插件页面上有关此内容的文档 http ://jquery.malsup.com/form/#file-upload?您无法访问 ajaxSubmit() 中的 JSON,因为响应实际上写入到用于上传的隐藏 iframe 中。
这是他在示例页面上使用的代码:
success
方法在这里对您来说很重要...请注意,他正在编写将数据
返回到页面以进行调试。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
success
method is what matters for you here...notice that he is writing the returndata
to the page for debugging purposes.