开发者

Getting rails JSON validation errors in jquery callback

I have a create action like so in a rails controller:

def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user
 开发者_运维问答 else
    respond_to do |format|
      format.json { render :json => @user.errors, :status => :unprocessable_entity }
    end
  end
end

And using jQuery's ajax method I have an error callback defined, but can't understand how to get hold of the JSON response – i.e the @user.errors For a success callback you get a 'data' param passed, but for error you only get (xhr, status, error).

How can I get those json @user.errors in my ajax error callback?


Good JavaScript debugging practice: use Firebug to put a breakpoint in your error callback and inspect the function's arguments, drilling down until you find what you need. If your function accepts all 3 arguments, it stands to reason that you should find your @user.errors in one of them. Based on my experience, it is probably in xhr.responseText, as mkoryak suggested.


here is what i did

var onError = function(xhr, status) {
        var error = (status != "timeout") ? xhr.responseText : "";
        //at this point eval error or json.parse it
    };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜