IE 在提交包含文件的 jQuery 多部分表单数据时尝试下载 json 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8151138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE tries to download json response while submitting jQuery multipart form data containing file
提问by snitko
I'm trying to submit a form with a filefield in it via jQuery.Form plugin, here's the code:
我正在尝试file通过 jQuery.Form 插件提交一个带有字段的表单,这是代码:
$('form').ajaxSubmit({
url: "/path",
dataType: "json",
contentType: "multipart/form-data"
...
The server then returns json as a response. Works great in all browsers except IE, which tries to download the response as a file. If I remove the file field from the form, it also works just fine.
然后服务器返回 json 作为响应。在除 IE 之外的所有浏览器中都能很好地工作,IE 会尝试将响应下载为文件。如果我从表单中删除文件字段,它也可以正常工作。
I've seen various solutions here and in Google and basically tried almost everything described, including setting enctypefor the form via jQuery, but it didn't work.
我在这里和谷歌中看到了各种解决方案,基本上尝试了几乎所有描述的方法,包括enctype通过 jQuery设置表单,但没有奏效。
Any suggestions would be very welcomed.
任何建议将非常受欢迎。
采纳答案by snitko
I have not found a direct solution to this, but I eventually implemented the following workaround: I used dataType: "text"in my ajax settings and then returned plaintext from controller, separating values with ;and parsing them on the client side. That way IE and Forefox stopped trying to download a response.
我还没有找到直接的解决方案,但我最终实现了以下解决方法:我dataType: "text"在我的 ajax 设置中使用,然后从控制器返回纯文本,;在客户端分离值并解析它们。这样 IE 和 Forefox 就停止尝试下载响应。
I did not find any other way to prevent said behavior other then to return plaintext. I tried returning JSON as plaintext and then parsing it with $.parseJSON, but it didn't work due to some js errors.
除了返回明文之外,我没有找到任何其他方法来防止上述行为。我尝试将 JSON 作为纯文本返回,然后用 $.parseJSON 解析它,但由于一些 js 错误,它没有工作。
回答by Dmitrii
You can simply return JSON from the controller as "text/html" and then parse it on the client side using JQuery.parseJSON().
您可以简单地从控制器以“text/html”形式返回 JSON,然后在客户端使用 JQuery.parseJSON() 对其进行解析。
Controller:
控制器:
return this.Json(
new
{
prop1 = 5,
prop2 = 10
},
"text/html");
Client side:
客户端:
jsonResponse = $.parseJSON(response);
if(jsonResponse.prop1==5) {
...
}
This solution has been working for me.
这个解决方案一直在对我来说有效。
回答by Sannek8552
Just send response with 'Content-Type', 'text/html'header.
只需发送带有'Content-Type', 'text/html'标头的响应。
回答by devilmark84
Just set Content-Type: text/html
刚设置 Content-Type: text/html
This happens because IE8 doesn't recognize application/...mimetype.
This works for me.
发生这种情况是因为 IE8 无法识别application/...mimetype。这对我有用。
Hope it helps.
希望能帮助到你。
回答by lboix
Same situation than you folks : the problem only occurs with enctype="multipart/form-data"form used with $(form).ajaxSubmit(...)function.
和你们一样的情况:问题只发生在enctype="multipart/form-data"与$(form).ajaxSubmit(...)函数一起使用的表单中。
My team and I had to replace (in this function) dataType: 'json'option with dataType: 'text'and add responseText = $.parseJSON(responseText);to force server response parsing.
我和我的团队必须替换(在此功能中)dataType: 'json'选项dataType: 'text'并添加responseText = $.parseJSON(responseText);以强制服务器响应解析。
Of course we also had to step in server side to return a response with "text/plain"header instead of "application/json"
当然,我们还必须进入服务器端以返回带有"text/plain"标头而不是标头的响应"application/json"
We're not proud of it :( IE is definitely killing everything...
我们并不为此感到自豪:(IE 肯定会杀死一切......
I didn't try the advice given by zmonteca (already spent too much time on it) but it seems worthy : let us know if it was OK for you.
我没有尝试 zmonteca 给出的建议(已经在它上面花费了太多时间),但它似乎值得:让我们知道它是否适合您。
Hope it helps!
希望能帮助到你!
回答by Vanya
if you work with Zend you can do
如果你使用 Zend,你可以做到
$this->getResponse()->setHeader('Content-Type', 'text/html');
in your controller action. and on client-side, in case of jQuery, you can do
在您的控制器操作中。在客户端,在 jQuery 的情况下,你可以做
data = $.parseJSON(data);
回答by sroes
I came up with the following workaround (in Zend Framework):
我想出了以下解决方法(在 Zend 框架中):
if (!$this->_request->isXmlHttpRequest()) {
die('<textarea>'.Zend_Json::encode($data).'</textarea>');
}
$this->view->assign($data);
回答by keza
This site has some info about wrapping the response in a http://forum.jquery.com/topic/jquery-form-malsup-ie7-file-download-security-warning-on-ajax-file-upload
这个站点有一些关于在http://forum.jquery.com/topic/jquery-form-malsup-ie7-file-download-security-warning-on-ajax-file-upload 中包装响应的信息
I seemed to be able to fix my problem by making my server return the JSON as a string. Then I used JSON.parse() inside the complete event.
我似乎能够通过让我的服务器将 JSON 作为字符串返回来解决我的问题。然后我在 complete 事件中使用了 JSON.parse()。
回答by Neha
Removing ContentType from serverside works for me.
从服务器端删除 ContentType 对我有用。

