我正在使用ajax提交我的注册表单。
详细信息正确时,表单正确提交。如果细节错误,我还能正确捕获错误。但是,如果细节错误,我也可以在我的控制台日志中看到POST http://localhost:3000/users 422 (Unprocessable Entity)
错误消息:
我的问题是,我应该担心这个错误吗?或者这是正常的? 如果这不正常,我该如何正确处理呢?
谢谢!
################################更新########### ###################
来自终端的错误消息:
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"=>"ryzalyusoff@gmail.com", "password"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.2ms) BEGIN
User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'ryzalyusoff@gmail.com' 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"
});
});
});
答案 0 :(得分:0)
你不要担心控制台中的422,这个红色警告不会破坏你的javascript代码。 但是这个错误可以让你在javascript代码中轻松验证处理,例如:
$('#form').on('ajax:error',
function () { return 'handle me'; }
);