Rails处理ExtJS上传的一个注意事项

本文解决了使用ExtJS上传组件时出现的下载对话框问题,通过调整服务器响应内容类型为text/html,成功避免了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用ExtJS的上传组件时,我在controller上通过如下代码进行响应:

render :json => {:success => @attachment.save, :message => 'succ'}.to_json

在上传时,总是出现下载对话框,下载的内容为json内容,十分奇怪,对比网上的例子, 响应基本是一样的,可是为什么别人都可以上传?
这问题搞了一上午,开始怀疑是extjs的一些plugin冲突,去掉一堆plugin后还是如此,后来想起可能是Ajax上传使用了iframe造成的,仔细看了一下ExtJS文档,看到如下内容:
[quote]
The server response is parsed by the browser to create the document for the IFRAME. If the server is using JSON to send the return object, then the Content-Type header must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.
[/quote]
晕,原来是这样,修改为如下代码:

render :text => {:success => @attachment.save, :message => 'succ'}.to_json

成功,不再出现下载json的对话框了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值