IE尝试在提交包含文件的jQuery多部分表单数据时下载json响应 [英] IE tries to download json response while submitting jQuery multipart form data containing file
问题描述
我正在尝试通过jQuery.Form插件提交一个带有文件
字段的表单,这里是代码:
$('form')。ajaxSubmit({
url:/ path,
dataType:json,
contentType :multipart / form-data
...
服务器然后返回json为在IE以外的所有浏览器中都能很好地工作,它试图将响应作为文件下载。如果我从表单中删除文件字段,它也可以正常工作。
<我在这里和谷歌都看到了各种解决方案,基本上几乎尝试了所有描述的内容,包括通过jQuery为表单设置
enctype
,但它没有用。 任何建议都会受到欢迎。
我还没找到直接解决这个问题,但我最终实现了以下解决方法:我在我的ajax设置中使用 dataType:text
然后返回明文从控制器,用;
分隔值并在客户端解析它们。这样IE和Forefox就不再试图下载响应了。
我没有找到任何其他方法来阻止所述行为,然后返回明文。我尝试将JSON作为纯文本返回,然后使用$ .parseJSON进行解析,但由于某些js错误,它无法正常工作。
I'm trying to submit a form with a file
field in it via jQuery.Form plugin, here's the code:
$('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.
I've seen various solutions here and in Google and basically tried almost everything described, including setting enctype
for the form via jQuery, but it didn't work.
Any suggestions would be very welcomed.
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.
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.
这篇关于IE尝试在提交包含文件的jQuery多部分表单数据时下载json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!