i want to do an ajax call to node.js with get method that the result of the ajax call will show in the same html page that i call the ajax function
this is the html code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sing-in Page</title>
<link rel="stylesheet" href="css/style.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(function() {
$("#myform").bind('ajax:complete', function() {
// tasks to do
alert('1');
});
});
</script>
</head>
<body>
<form method="get" action="http://localhost:8080/Login" id="myform" class="login">
<p>
<label for="login">Email:</label>
<input type="text" name="login" id="login" value="">
</p>
<p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" value="">
</p>
<p class="login-submit">
<button type="submit" class="login-button">Login</button>
</p>
<p class="forgot-password"><a href="index.html">Forgot your password?</a></p>
</form>
</body>
</html>
the is my node.js code, i know that i need to write something in the res.end function but i don't now what..
var express = require('express');
var app = express();
var fs = require("fs");
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'XXXX',
user : 'XXXX',
password : 'XXXX',
database : 'XXXX',
});
app.use(express.bodyParser());
app.get('/Login', function(req, res) {
pool.getConnection(function(err, connection) {
var sql = 'select count(id) as ok from users where email=\''+req.body.login+'\' and password=\''+req.body.password+'\';';
console.log("query: "+sql);
connection.query( sql, function(err, rows) {
console.log(rows[0].ok);
connection.end();
});
});
// what to write here that res.end() will return the result of console.log(rows[0].ok);
res.end();
});
app.listen(8080, function() {
console.log('Server running...');
});
i want to get the result of the sql query from the DB