【发布时间】:2014-08-04 03:24:54
【问题描述】:
我想使用 get 方法对 node.js 进行 ajax 调用 ajax 调用的结果将显示在我调用 ajax 函数的同一个 html 页面中
这是html代码:
<!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>
这是我的 node.js 代码,我知道我需要在 res.end 函数中写一些东西,但我现在不知道..
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...');
});
我想从数据库中获取 sql 查询的结果
【问题讨论】:
标签: javascript ajax html node.js