javascript 使用 node.js 从服务器获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24207507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get data from server with node.js
提问by royb
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
我想使用 get 方法对 node.js 进行 ajax 调用,ajax 调用的结果将显示在我调用 ajax 函数的同一个 html 页面中
this is the html code:
这是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>
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..
这是我的 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...');
});
i want to get the result of the sql query from the DB
我想从数据库中获取 sql 查询的结果
回答by Victor Parmar
You almost had it - just add res.send() after closing the connection :)
您几乎拥有它 - 只需在关闭连接后添加 res.send() :)
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();
res.send(rows[0].ok); // you simply send your data after you're done your processing
});
});
});
On the client side you can bind the success event
在客户端,您可以绑定成功事件
$("#myform").bind('ajax:success', function(result) {
alert(result);
});
回答by Ferrakkem Bhuiyan
If you tryiny to trigger the data event on the server, you have to send data to the server, maybe using Jquery's post method as in:
如果您尝试在服务器上触发数据事件,则必须将数据发送到服务器,可能使用 Jquery 的 post 方法,如下所示:
var postSomething = function(){
$.post
(
"http://127.0.0.1:3000/",
{wooo:'wooo'},
function(data)
{
console.log(data);
}
);
}
when 'data' event, you should write some data in the response object:
当 'data' 事件发生时,您应该在响应对象中写入一些数据:
req.on('data', function(sock) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("Hello!!");
});
回答by danilodeveloper
Send the HTTP Get to server:
将 HTTP Get 发送到服务器:
$.get( "YOUR_URL_HERE/Login", function( data ) {
alert( data );
});
To return a Json result, you can do something like this:
要返回 Json 结果,您可以执行以下操作:
app.get('/Login', function(req, res) {
var result;
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);
result = rows[0].ok;
connection.end();
});
});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.json({ status: result })
});
回答by Vivek Sharma
You could use something like this:
你可以使用这样的东西:
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();
var returnData = {};
returnData.message = rows[0].ok;
return res.json(200, returnData);
});
});
});