Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当从控制器返回Json时,浏览器希望将Json作为文件下载_Json_Asp.net Mvc 3 - Fatal编程技术网

当从控制器返回Json时,浏览器希望将Json作为文件下载

当从控制器返回Json时,浏览器希望将Json作为文件下载,json,asp.net-mvc-3,Json,Asp.net Mvc 3,控制器代码: $("#frmCompose").submit(function () { $(this).ajaxSubmit({ success: function (response) { alert('success'); } }); }); 查看代码: [HttpPost] public Ac

控制器代码:

  $("#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" });
            }
        }

发送
@Label(“To:”)
@Html.TextBox(“txtTo”)
@标签(“主题:”)
@Html.TextBox(“txtSubject”)
@Label(“Body:”)
@Html.TextArea(“txtBody”)
在我的控制器中,我返回一个带有文本消息的JSon结果。 为什么FireFox中的视图希望将json作为文件下载

我所要做的就是确保在成功回调中得到响应,根据:

因为无法上传 使用浏览器的 XMLHttpRequest对象,表单插件 使用隐藏的iframe元素提供帮助 完成任务。这是一个常见的问题 技术,但它有内在的 局限性iframe元素是 用作窗体的目标 提交操作,这意味着 服务器响应将写入 iframe。这是好的,如果响应 类型是HTML或XML,但不起作用 如果响应类型是script,也可以 或者JSON,两者通常都包含 需要重复的字符 在中找到实体引用时使用实体引用 HTML标记

考虑到 脚本和JSON响应 插件允许对这些响应进行修改 嵌入在textarea元素中,并且 建议您这样做 这些响应类型在中使用时 与文件上传合并


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

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

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

<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>

当您进行AJAX调用时,或者当您转到浏览器地址栏中的地址时,您是否看到了这一点?您可以发布控制器代码吗?或者在ajax请求中指定
dataType:'json'
。@DanielB-我已经在原始帖子中添加了一个编辑。@dlev-它发生在ajax调用中,而不是在浏览器地址上。barI解决了这个问题,我需要在.submit()函数中返回false,看起来还可以!
$("#frmCompose").submit(function () {

            // submit data to server here....


            return false;
        });