2016-04-27 49 views
-1

我正在发送此请求,只要成功,就完全正常。但是,如果我尝试使用现有的用户名或电子邮件创建用户,我应该得到400 bad request,并回复详细说明问题所在。AJAX请求不会在400错误查询上执行失败

事情是,当我发送此请求并获得400,没有任何东西写到控制台/控制台(甚至没有写入'fail!'字符串),但我可以看到响应在控制台/网络选项卡中是正确的,因此问题可能不是在后端。

$("#registerForm").submit(function(e){ 
    e.preventDefault() 
    $.ajax({ 
    type: "POST", 
    url: "./register", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    data: JSON.stringify({ 
     "username": formcontent['username'], 
     "email": formcontent['email'], 
     "password": formcontent['password'], 
    }), 
    success: function(e) { 
     window.location.href = './login' 
     return false 
    }, 
    fail: function(e) { 
     console.log('fail!') 
     console.log(e) 
     //react to error 
     return false 
    }, 
    }) 
}) 
+0

尝试'错误尝试:在''代替失败:'..或更好的'$阿贾克斯( {})。done(function(){})。fail(function(){});' – Spluf

+0

是的,工作。在问之前我应该​​多研究一下。 – Zygro

回答

1

我想 '失败' 是一个无效的功能,请在下面的结构

$.ajax({ 
type : "POST", 
url : "/register", 
contentType : "application/json; charset=utf-8", 
dataType : "json", 
data : JSON.stringify({ 
    "username" : formcontent['username'], 
    "email" : formcontent['email'], 
    "password" : formcontent['password'], 
}), 
success : function(response) { 
    console.log(response); 
}, 
error : function(response, status, err) { 
    console.log(response); 
    console.log(status); 
    console.log(err); 
} 
}); 
相关问题