我正在使用远程表单进行ajax调用。设计返回401未经身份验证的响应,浏览器控制台也显示401什么是正确的状态。但是在java脚本代码中,成功回调总是执行。
function initFileAttachmentForm() {
$('#new_file_form').on('ajax:before', function () {
// Do some Work
}).on('ajax:success', function (e, data, status, xhr) {
debugger;
// This is Executing on Authentication fails
// When i Logged out from another tab
}).on('ajax:error', function (e, xhr) {
debugger;
// This should Executes on authentication fail
});
}
浏览器控制台显示正确的响应。
答案 0 :(得分:1)
解决此问题的更好方法是在任何表单提交和任何其他服务器调用之前进行ajax调用(IsUserSignedIn
)。什么是解决与401错误处理产品相关的问题。
// Do some processing before
isUserSignedIn(function () {initFileAttachmentForm()});
// Some processing after
//definition of isUserSignedIn
function(callback) {
$.ajax({
url: "user/is_signed_in",
type: 'POST',
beforeSend: function (xhr) {
console.log('Before Send');
},
success: function(result) {
if(result.is_signed_in) {
callback();
} else {
//401 error handing code
}
},
error: function (result) {
console.log('Error');
}
});
}
答案 1 :(得分:0)
我认为你正在使用某种宝石,这导致了这个错误。
您最近是否更新了此列表中的任何gem:jquery-ujs / jquery-form-rails / jquery-ui-rails / jquery-rails
相反将ajax事件绑定到表单,尝试单独调用ajax来提交数据:
$.ajax({
url: "foo/bar",
type: 'POST',
data: {foo: 'foo', bar: 'bar'},
beforeSend: function (xhr) {
console.log('Before Send');
},
success: function(result) {
console.log('Success');
},
error: function (result) {
console.log('Error');
}
});