IE要下载JSON结果.... MVC3 [英] IE wants to download JSON result....MVC3
问题描述
从用户到我们的服务器我MVC3应用程序的上传文件。我返回JsonResult显示任何错误,如果有的话:
My MVC3 app uploads documents from the user to our server. I am returning a JsonResult to display any errors, if any:
[HttpPost]
public JsonResult SaveDocument(DocumentModel model, HttpPostedFileBase postedFile)
{
//my wonderful code
return Json(new { success = true, message="ok" });
}
继承人如何提交请求:
Heres how i submit the request:
var isSubmitting = false;
var addDocumentOptions = {
beforeSubmit: beforeAddDocumentSubmit, // pre-submit callback
success: afterDocumentSubmit // post-submit callback
};
$('#btnCreateDocument').click(function (e) {
e.preventDefault();
$('#divError').html('');
if (!isSubmitting) {
$('#createDocForm').submit();
}
});
此JavaScript函数的上传完成后运行:
This javascript function runs when the upload is complete:
function afterDocumentSubmit(responseText, statusText, xhr, $form) {
if (responseText.success) {
//no errors
} else {
$('#divError').html('Error: ' + responseText.message);
}
}
在FF,铬等,我的JavaScript code运行正常,但在IE浏览器,浏览器要下载JSON结果为文本。我得到一个下载/打开文件对话框不应该出现的对话框。如何让我的IE不能下载我的JSON结果和行为像其他浏览器?谢谢
In FF, Chrome etc., my javascript code runs fine, but in IE, the browser wants to download the Json result as text. I get a download/open file dialog box that shouldnt appear. How do i make IE not download my Json result and behave like the other browsers? Thanks
推荐答案
我遇到了类似的问题做同样的Spring MVC中爪哇。问题是,春天是返回的Content-Type JSON结果为应用程序/ JSON
,这似乎让IE要下载它。你可以尝试改变内容类型为文本/纯
; IE不会提示你下载的文件在这种情况下。我怀疑,类似的东西可能会发生在这里。
I ran into a similar problem doing the same in Spring MVC on Java. The problem was that Spring was returning the content-type of the JSON result as application/json
, which seems to make IE want to download it. You can try changing the content-type to text/plain
; IE won't prompt you to download the file in this case. I suspect that something similar might be happening here.
您可以尝试:
return Json(new { success = true, message = "ok" }, "text/plain");
在应对新问题:问题是,的responseText
只是一个字符串。你需要做的是将其转换成一个JavaScript对象。你可以这样说:
In response to your new problem: the issue is that responseText
is just a string. What you need to do is to convert it into a Javascript Object. You can do it like this:
var response = JSON.parse(responseText);
if(response.success) {
...
}
大多数浏览器都支持 JSON.parse()来
。如果您在使用不兼容的浏览器的问题,你可以随时使用 JSON JavaScript库的。
Most browsers support JSON.parse()
. If you're having issues with non-compliant browsers, you can always use the JSON Javascript Library.
这篇关于IE要下载JSON结果.... MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!