jquery ajax 422在Chrome中取得了成功

ear*_*old 8 google-chrome ruby-on-rails jquery-forms-plugin

我有一个使用rails3,jquery-forms的网站,我正在使用firefox和chome进行测试.

为了测试,我每次都让服务器返回422状态.

当我提交表单时,Firefox正确地发出"错误".Chrome错误地击中了"成功".

任何人都有任何想法为什么会这样?

$('form').ajaxSubmit({
    dataType: 'json',
    success: function(responseText, statusText, xhr, $form) {
        console.log("It hits success");         
    },
    error: function(responseText, statusText, xhr) {
            console.log("It hits failure");
    }
    });
Run Code Online (Sandbox Code Playgroud)

bas*_*gys 0

我假设您的服务器代码如下所示:

def update
 @model = Model.find(params[:id])
 @model.update_attributes(params[:model])
 if @model.save
   render :json => @model, :status => :ok
 else
   head :unprocessable_entity # aka 422 status code
 end
end
Run Code Online (Sandbox Code Playgroud)

那么首先你的ajax请求中是否涉及到文件上传?显然,在这种情况下不能使用 HTTP 状态代码。JQuery 论坛上的主题

成功回调的条件是 2xx 状态或 304(未修改)。