浏览器想要从控制器返回 Json 时将 Json 作为文件下载

发布于 2024-11-07 20:50:22 字数 1525 浏览 0 评论 0原文

  $("#frmCompose").submit(function () {

            $(this).ajaxSubmit({

                success: function (response) {
                    alert('success');
                }
            });

        });

控制器代码:

 [HttpPost]
        public ActionResult SendEmail(EmailMessageModel emailMessage)
        {
            try
            {
                // do something with the data
                return Json(new StatusModel { error = false });
            }
            catch (Exception)
            {
                return Json(new StatusModel { error = true, message = "Could not send email" });
            }
        }

查看代码:

<form id="frmCompose" method="post" action="SendEmail">
    <button id="compose" class="btn-pencil">
        Send</button>
    <div class="fields-inline">
        <div class="editor-label">
            @Html.Label("To:")
        </div>
        @Html.TextBox("txtTo")
    </div>
    <div class="fields-inline">
        <div class="editor-label">
            @Html.Label("Subject:")
        </div>
        @Html.TextBox("txtSubject")
    </div>
    <div class="fields-inline">
        <div class="editor-label">
            @Html.Label("Body:")
        </div>
        @Html.TextArea("txtBody")
    </div>
</form>

在我的控制器中,我返回带有文本消息的 JSON 结果。 为什么FireFox中的视图要将json作为文件下载来下载?

我想要做的就是确保在成功回调中得到响应

  $("#frmCompose").submit(function () {

            $(this).ajaxSubmit({

                success: function (response) {
                    alert('success');
                }
            });

        });

Controller code:

 [HttpPost]
        public ActionResult SendEmail(EmailMessageModel emailMessage)
        {
            try
            {
                // do something with the data
                return Json(new StatusModel { error = false });
            }
            catch (Exception)
            {
                return Json(new StatusModel { error = true, message = "Could not send email" });
            }
        }

View Code:

<form id="frmCompose" method="post" action="SendEmail">
    <button id="compose" class="btn-pencil">
        Send</button>
    <div class="fields-inline">
        <div class="editor-label">
            @Html.Label("To:")
        </div>
        @Html.TextBox("txtTo")
    </div>
    <div class="fields-inline">
        <div class="editor-label">
            @Html.Label("Subject:")
        </div>
        @Html.TextBox("txtSubject")
    </div>
    <div class="fields-inline">
        <div class="editor-label">
            @Html.Label("Body:")
        </div>
        @Html.TextArea("txtBody")
    </div>
</form>

In my controller I return a JSon result with a text message.
Why does the view in FireFox want to download the json as a file download?

All I want to do is ensure I get a response within the success callback

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-11-14 20:50:22

解决方案是在表单的 Submit() 调用函数中返回 false。

这样,json 结果将在提交函数中使用,而不是传递给浏览器进行处理。

$("#frmCompose").submit(function () {

            // submit data to server here....


            return false;
        });

Solution is to return false within the submit() call function for the form.

That way, the json result is consumed within the submit function and not passed to the browser for handling.

$("#frmCompose").submit(function () {

            // submit data to server here....


            return false;
        });
禾厶谷欠 2024-11-14 20:50:22

根据文档

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

应对挑战
脚本和 JSON 响应,表单
插件允许这些响应
嵌入到 textarea 元素中
建议您这样做
这些响应类型在使用时
与文件上传结合。

这基本上意味着,如果您想使用 jquery 表单插件上传文件,并且您的表单包含文件输入字段,则服务器需要将返回的 JSON 包装到

According to the documentation:

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.

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.

This basically means that if you want to upload files using the jquery form plugin and your form contains file input fields the server needs to wrap the returned JSON into <textarea> tags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文