---
title: "Rails处理ExtJS上传的一个注意事项"
author: "crazycode"
date: "2009-07-06T15:24:45Z"
tags: []
categories: ["Web前端"]
slug: "rails-extjs"
draft: false
lastmod: "2009-07-06T15:24:45Z"
id: 420838
url: "https://crazycode.iteye.com/blog/420838"
---

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

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

```

在上传时，总是出现下载对话框，下载的内容为json内容，十分奇怪，对比网上的例子， 响应基本是一样的，可是为什么别人都可以上传？
这问题搞了一上午，开始怀疑是extjs的一些plugin冲突，去掉一堆plugin后还是如此，后来想起可能是Ajax上传使用了iframe造成的，仔细看了一下ExtJS文档，看到如下内容：

> 
> 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.
> 

晕，原来是这样，修改为如下代码：

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

```

成功，不再出现下载json的对话框了。