jQuery ajax success/done will not run callbacks if request is json but the response is empty (typical 200)

When using

var onDone = function() { ... }
var onFail = function() { ... }
var params = { ... }
var url = ...

$.ajax({
  type:  'put',
  url:  url,
  contentType: 'application/json; charset=utf-8',
  data:  JSON.stringify(params),
})
.done(onDone)
.fail(onFail);

Always make sure your rails controller returns something:

if @property.update_attributes(params[:property])
  render :json => { :ok => true }, :status => :ok
else
  render :json => { :ok => false }, :status => :unprocessable_entity
end

Do not use
head :ok
render :nothing => true, :status => :ok

Or else jQuery won't run his callbacks properly

Pedro Cunha almost 9 years ago
This website uses short-lived cookies to improve usability.
Accept or learn more