jQuery表单上传错误 [英] jQuery form upload error

查看:14
本文介绍了jQuery表单上传错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个版本的 jQuery 表单插件 https://raw.github.com/malsup/form/master/jquery.form.js提交时出错:

I am using this version of jQuery form plugin https://raw.github.com/malsup/form/master/jquery.form.js getting an error on submit:

form.submit();

SCRIPT87: Invalid argument. jquery.form.js, line 347 character 5

我的代码:

<form id="ajaxUploadForm" action="@Url.Action("Upload", "Home")%>" method="post" enctype="multipart/form-data" >
    <fieldset>
        <legend>Upload a file</legend>
        <label>File to Upload: <input type="file" name="file" /></label>
        <input id="ajaxUploadButton" type="submit" value="Upload" />
    </fieldset>
</form> 

$(function () {
        $("#ajaxUploadForm").ajaxForm({
            iframe: true,
            dataType: "json",
            beforeSubmit: function () {
               // $("#ajaxUploadForm").block({ message: '<img src="/Content/themes/start/images/progress.gif" />' });
            },
            success: function (result) {
               // $("#ajaxUploadForm").unblock();
                //$("#ajaxUploadForm").resetForm();
                $.growlUI(null, result.message);
            },
            error: function (xhr, textStatus, errorThrown) {
                //$("#ajaxUploadForm").unblock();
                //$("#ajaxUploadForm").resetForm();
                $.growlUI(null, 'Error uploading file');
            }
        });
    });

我正在侧面的简单模型对话框中进行此上传.

I am doing this upload in side simple model dialog.

可能有人知道如何解决这个问题?

May be some one may have any ideas how ti fix that?

推荐答案

如果您要发布到的控制器操作返回 JSON,您可能需要将其包装在 <textarea> 标记中在文档中解释:

If the controller action that you are POSTing to is returning JSON you might need to wrap it in a <textarea> tags as explained in the documentation:

由于无法上传使用浏览器的文件XMLHttpRequest 对象,表单插件使用隐藏的 iframe 元素来帮助与任务.这是一个常见的技术,但它有内在的限制.iframe 元素是用作表单的目标提交操作,这意味着服务器响应被写入框架.这很好,如果响应类型是 HTML 或 XML,但不起作用以及如果响应类型是脚本或 JSON,这两者通常都包含需要reresented的字符在找到时使用实体引用HTML 标记.

Since it is not possible to upload files using the browser's XMLHttpRequest object, the Form Plugin uses a hidden iframe element to help with the task. This is a common technique, but it has inherent limitations. The iframe element is used as the target of the form's submit operation which means that the server response is written to the iframe. This is fine if the response type is HTML or XML, but doesn't work as well if the response type is script or JSON, both of which often contain characters that need to be repesented using entity references when found in HTML markup.

为了应对挑战脚本和 JSON 响应,表单插件允许这些响应嵌入在 textarea 元素中,它建议您这样做这些响应类型在使用时与文件上传结合.请但是请注意,如果没有在表单中输入文件,然后请求使用普通 XHR 提交表单(不是 iframe).这使你的服务器代码负担要知道何时使用 textarea 何时不使用到.如果你喜欢,你可以使用要强制的插件的 iframe 选项它总是使用 iframe 模式和那么你的服务器总是可以嵌入在文本区域中响应.

To account for the challenges of script and JSON responses, the Form Plugin allows these responses to be embedded in a textarea element and it is recommended that you do so for these response types when used in conjuction with file uploads. Please note, however, that if there is no file input in the form then the request uses normal XHR to submit the form (not an iframe). This puts the burden on your server code to know when to use a textarea and when not to. If you like, you can use the iframe option of the plugin to force it to always use an iframe mode and then your server can always embed the response in a textarea.

这篇关于jQuery表单上传错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆