I have tried for hours to get the value from my function after validation but it just does not seem to work. I have tried looking at similar threads but still did not manage to understand their answers. This is my code.
/*$("#submit").click(function(){
// Set form variable
var form = $('#loginform');
form.submit(function(event){
var userid = $('#userID').trim(userid);
var password = $('#Password').trim(password);
// Process AJAX request
var validate = $.post('http://localhost:3000/api/login',
$("loginform").serialize(), function(data){
alert(data);
});
// Prevent default form action
event.preventDefault();
});
}); */
function ajaxcall(url, data, callback) {
$.ajax({
"url": "http://localhost:3000/api/login", // server url
"type": 'POST', //POST or GET
"data": "#loginform", // data to send in ajax format or querystring format
"datatype": 'json',
"beforeSend": function() {
alert('sending data');
// do some loading options
},
success: function(data) {
if(data=="Login Success"){
callback(true); // return data in callback
} else {
callback(false);
}},
complete: function() {
alert('ajax call complete');
// success alerts
},
error: function(xhr, status, error) {
alert(xhr.responseText); // error occur
}
});
}
The first part which is commented is basically what I have tried and also failed for me so I decided to use $.ajax
instead but it seems like there is no change. This is my html code.
<form method="POST" action="index.html" id="loginform" onsubmit="return ajaxcall(callback)" >
I am looking for some code for me to refer to as a reference or a simple explaination etc. I have looked at this before and tried but it is not working so I am guessing it has to do with my code itself rather than html. Do forgive me for asking the same question but I am pretty much stuck after reading through this thread for hours trying to understand.
How do I return the response from an asynchronous call?
Server code:
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/myproject';
var authenticate = function(db, req, callback){
var cursor = db.collection('LoginID').find({"_id" : req.body.userID,
"Password" : req.body.Password
}).count(function(err,doc){
if(err) return callback(err);
if(doc == 0){
console.log('Invalid Login ID or Password');
return callback(null, doc);
} else {
console.log('Login Success');
return callback(null, doc);
}
});
}
module.exports = {
postCollection : function(req,res){
var username = req.body.userID;
var Password = req.body.Password;
//var createdDate = "<b>" + day + "/" + month + "/" + year + "</b>"
MongoClient.connect(url, function(err, db) {
//assert.equal(null, err);
if(err) {
res.send(err);
res.end();
}
authenticate(db, req, function(err,doc) {
if(err)
res.send(err);
else{
if(!doc){
res.send( 'Invalid Login ID or Password' );
res.end();
} else {
res.send("Login success")
res.end();
}
}
db.close();
});
});
}
}
PS: This is for calling my api which then checks the database for the entry of the user and if it is not there it outputs a response "Invalid Login ID or password" and stays on the same page but if there is an entry of the user, it would then output"Login Success" which would then be redirected to index.html
. Sorry if I was unclear.