我正在使用ajax提交我的註冊表單。我應該擔心這個422(Unprocessable Entity)錯誤嗎? (Rails&Devise)
提交正確時,信息是正確的形式。如果細節錯誤,我也能夠正確捕獲錯誤。但是,如果信息有誤,我也可以看到POST http://localhost:3000/users 422 (Unprocessable Entity)
錯誤消息對我的控制檯日誌:
我的問題是,我應該擔心這個錯誤?或者這是正常的? 如果這不正常,我該如何正確處理它?
謝謝!
################################ UPDATE ############ ##################從終端
錯誤消息:
Started POST "/users" for ::1 at 2016-06-15 09:20:20 +0100
Processing by RegistrationsController#create as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0D1qSK8IfbuefQ0bwEdKQFD+6WZ8XJtCnl2FZ6Nln9khLtv6qYCmoTKhwBFkVIJdYXSRQJ4e+9QAAdJ5UJzukQ==", "user"=>{"first_name"=>"John", "last_name"=>"Doe", "email"=>"[email protected]", "password"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.2ms) BEGIN
User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY '[email protected]' LIMIT 1
(0.2ms) ROLLBACK
Completed 422 Unprocessable Entity in 93ms (Views: 0.4ms | ActiveRecord: 1.1ms)
註冊控制器:
class RegistrationsController < Devise::RegistrationsController
respond_to :json
def create
super
end
end
周的application.js:
$(function(){
$("form#ajax_signup").submit(function(e){
e.preventDefault();
var user_info = $(this).serializeObject();
console.log("About to post to /users: " + JSON.stringify(user_info));
$.ajax({
type: "POST",
url: "http://localhost:3000/users",
data: user_info,
success: function(json){
console.log("The Devise Response: " + JSON.stringify(json));
},
error: function(xhr) {
var errors = jQuery.parseJSON(xhr.responseText).errors;
for (messages in errors) {
error_messages = messages + ' ' + errors[messages];
console.log(error_messages);
}
},
dataType: "json"
});
});
});
難道你把相關日誌條目的其餘部分,並在控制器方法負責(包括使用的任何強大的PARAMS方法)。 – Matt
是的,當然你應該關心,當然由於某種原因,這個POST操作失敗(「收到電子郵件地址」)會返回一些4xx響應。你能指望什麼?你只是想做客戶端驗證?即使如此,你應該處理在任何情況下由服務器發送的錯誤響應... – matthias
@Matt我已經發布了一些關於該問題的更多詳細信息 –