浏览器想要从控制器返回 Json 时将 Json 作为文件下载
$("#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案是在表单的 Submit() 调用函数中返回 false。
这样,json 结果将在提交函数中使用,而不是传递给浏览器进行处理。
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.
根据文档:
这基本上意味着,如果您想使用 jquery 表单插件上传文件,并且您的表单包含文件输入字段,则服务器需要将返回的 JSON 包装到
According to the documentation:
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.