javascript - AJAX success:function always returning success (and 200 status code), error function not triggering -
i'm bit of beginner when comes ajax calls apologies in advance because i'm stuck , have no idea why!
in summary, have own server displaying log in page. using router determining whether or not details entered in form correct user proceed homepage. log in button linked ajax whereby add event handler [should] displays alert boxes based on outcome of check in router.
so.. problem displaying correct alert boxes each response. 'success' alert box displaying status code of 200. code error:function not being reached.
i have router file serve pages , deal log in check, js file containing correct combinations of log in credentials, login html page , login js file. using javascript , ajax , google chrome debugging. of code think need question below. please ask if require more.
login check on router:
app.post('/api/login/', function (req, res) { console.log(req.body); var emaillog = req.body.email; var passwordlog = req.body.password; validuser = false; // search user data (var x in users.users) { if (emaillog === users.users[x].email && passwordlog === users.users[x].password) { validuser = true; break; } } if (validuser === true) { console.log("log in successful"); res.send('success'); } else { console.log("incorrect combination"); res.send('error'); } });
login.js file carrying out ajax stuff:
$(document).ready(function(){ console.log("in doc ready"); $("#submit_button").click(function(e){ console.log("in event handler"); e.preventdefault(); $.ajax({type: "post", url: "/api/login", data: { email: $("#email").val(),password: $("#password").val() }, success:function(data, textstatus, xhr){ alert("successful"); console.log(xhr.status); console.log(data); }, error:function(data){ alert("unsuccessful"); //$("#errormessage").html("incorrect email or password - try again"); } }); }); });
help! i've looked through several pages of ajax questions on here , none of them have worked me.. add error text div on log in page, happy displaying correct alert!!! ... why returning success alert box if type in incorrect combination? suggestions or pointers right direction fixing error?
update shows in console developer tools both correct combination , incorrect combination:
login.js:2 in doc ready login.js:5 in event handler login.js:18 200 login.js:20 success login.js:5 in event handler login.js:18 200 login.js:20 error
and in cmd window:
example app listening @ http://:::3000 { email: 'a@a.com', password: 'a' } a@a.com valid user : true log in successful { email: 'a@a.com', password: 't' } a@a.com t valid user : false incorrect log in combination - re-directing login page
in terms of ajax, long status "200" returned, regarded "success" , error function not fire. has nothing send response, it's http status code.
so adjust success function:
success:function(data, textstatus, xhr){ console.log(xhr.status); console.log(data); if ( data == "success" ) // thats send in case of proper login alert("successful") },
Comments
Post a Comment