读取控制器Rails返回的json数据

时间:2011-07-20 09:25:14

标签: jquery ruby-on-rails-3

我正在使用jquery文件上传插件。 here

我上传文件并生成json响应,如下所示:

 @upload = Upload.new(params[:upload])
    respond_to do |format|
      if @upload.save
        format.json {render :json => [ @upload.to_jq_upload ].to_json}
      end

我想要做的是在json对象发回时从json对象获取信息,但我无法确定它的发送位置。任何人都可以帮助弄清楚如何获取这些信息吗?

我的application.js中还有一个回调函数,如下所示:

$('#fileupload').bind('fileuploaddone', function (e, data) { }

我查看了这些数据,但我无法弄清楚如何解析它。如果我做data.url它给我删除没有id的URL,所以这是上传数据。我想要的是解析返回的数据。例如data.result但这只是给了我对象Object。

任何帮助都将不胜感激。

以下是观点:

<div id="fileupload">
    <%= form_for @upload, :html => { :multipart => true } do |f| %>
        <div class="fileupload-buttonbar">
            <label class="fileinput-button">
                <span>Add files... or drop them to upload</span>
                <%= f.file_field :photo, :id => "upload_photo" %>                
            </label>
        </div>
    <% end %>
    <div class="fileupload-content">
        <table class="files"></table>
        <div class="fileupload-progressbar"></div>
    </div>
</div>

<script id="template-upload" type="text/x-jquery-tmpl">
    <tr class="template-upload{{if error}} ui-state-error{{/if}}">
        <td class="preview"></td>
        <td class="name">${name}</td>
        <td class="size">${sizef}</td>
        {{if error}}
            <td class="error" colspan="2">Error:
                {{if error === 'maxFileSize'}}File is too big
                {{else error === 'minFileSize'}}File is too small
                {{else error === 'acceptFileTypes'}}Filetype not allowed
                {{else error === 'maxNumberOfFiles'}}Max number of files exceeded
                {{else}}${error}
                {{/if}}
            </td>
        {{else}}
            <td class="progress"><div></div></td>
            <td class="start"><button>Start</button></td>
        {{/if}}
        <td class="cancel"><button>Cancel</button></td>
    </tr>
</script>
<script id="template-download" type="text/x-jquery-tmpl">
    <tr class="template-download{{if error}} ui-state-error{{/if}}">
        {{if error}}
            <td></td>
            <td class="name">${name}</td>
            <td class="size">${sizef}</td>
            <td class="error" colspan="2">Error:
                {{if error === 1}}File exceeds upload_max_filesize (php.ini directive)
                {{else error === 2}}File exceeds MAX_FILE_SIZE (HTML form directive)
                {{else error === 3}}File was only partially uploaded
                {{else error === 4}}No File was uploaded
                {{else error === 5}}Missing a temporary folder
                {{else error === 6}}Failed to write file to disk
                {{else error === 7}}File upload stopped by extension
                {{else error === 'maxFileSize'}}File is too big
                {{else error === 'minFileSize'}}File is too small
                {{else error === 'acceptFileTypes'}}Filetype not allowed
                {{else error === 'maxNumberOfFiles'}}Max number of files exceeded
                {{else error === 'uploadedBytes'}}Uploaded bytes exceed file size
                {{else error === 'emptyResult'}}Empty file upload result
                {{else}}${error}
                {{/if}}
            </td>
        {{else}}
            <td class="preview">
                {{if thumbnail_url}}
                    <a href="${url}" target="_blank"><img src="${thumbnail_url}"></a>
                {{/if}}
            </td>
            <td class="name">
                <a href="${url}"{{if thumbnail_url}} target="_blank"{{/if}}>${name}</a>
            </td>
            <td class="size">${sizef}</td>
            <td colspan="2"></td>
        {{/if}}
        <td class="delete">
            <button data-type="${delete_type}" data-url="${delete_url}">Delete</button>
        </td>
    </tr>
</script>

2 个答案:

答案 0 :(得分:1)

我不熟悉红宝石。但是,在 fileuploaddone 绑定中,“data”对象包含要直接解析或访问的数据。

data.jqXHR.responseText 应包含返回数据的json字符串。有了它,你可以(至少在Javascript中)调用以下内容(取决于你对JSON的拥有)来获取JSON对象:

jQuery.parseJSON(data.jqXHR.responseText)

- 或 -

JSON.parse(data.jqXHR.responseText)

或者(,更容易),您可以查看 data.result 是否有效。如果是这样,它看起来像:

[Object {name =“image.jpg”,size = 43554,type =“image / jpeg”,more ...}]

要访问文件名(例如),您可以通过以下方式获取:

<强> data.result [0]。名称

希望这有帮助。

答案 1 :(得分:0)

请在你的控制器中试试。

render :json => { files: [@upload.to_jq_upload] }