I have a Backbone.js on Rails application, and I wanted to upload an image with AJAX. What’s weird is that the image is uploading to my Amazon S3, but ajax is throwing a parse error.
= form_for @page, :html => { :multipart => true, :data => { :type => 'json' } }, :remote => true do |f|
.field
= f.label :bg_image
= f.file_field :bg_image
.actions
= f.submit 'Save'
When I click save, it uploads fine (which I know because when I refresh it shows the new image, and there isn’t an error in my rails console). However, I want to bind the new image to my backbone model, but the following just results with a parseerror in JS console.
$("form").bind "ajax:success", (event, data) ->
log data
$("form").bind "ajax:error", (jqXHR, textStatus, errorThrown) ->
log jqXHR
log textStatus
log errorThrown
Turns out I needed
:format => :jsonin my form, in order to make the action url json (this is in addition to the ‘data-type’ => ‘json’).It was redirecting, because the html response is a redirect, hence the parseerror.