编辑: 在点击javascript网页上的按钮(代码示例#1)后,如何从我的node.js webapp服务(代码示例#2)获得响应。警报是空白的,我希望request.responsetext显示出来。
我正在使用网站查看特定视频的网站,该网站会在查看后将信息发送到以node.js编写的网络应用。
node.js中的web应用程序将根据发送的url更新数据库或以xml格式返回数据库列表。
注册公司通过互联网访问node.js应用程序并获取信息就好了。
我遇到的主要问题是在网站上观看视频是在用户按下按钮后,更新数据库的网页得到了响应,因此我可以确保数据库已更新。
代码示例#1(来自最终用户的网站)
发送的网址看起来像http://xxxctdb.yyyyy.com:1337/?c=u&r=3&s=c&id=cry12011999last46
然后我按下页面上的按钮,触发函数submitclick()并发送请求。一旦readystate = 4,那么按钮就会消失,我想要一个警报来显示响应是好还是坏或只是'谢谢'这个词。
<script>
function submitclick() {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 ) {
var div = document.getElementById('Button');
div.style.display = 'none';
alert (request.responseText);
}
};
request.open("GET", "Http://xxxctdb.yyyyyy.com:1337?c=u&r="+getQueryVariable("r")+"&s=c&id="+getQueryVariable("id"), true);
request.send();
}
</script>
代码示例#2(删除了无关代码的node.js,即切换下的许多其他情况)
var mysql = require('mysql');
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var o2x = require('object-to-xml');
var app = express();
var pool = mysql.createPool({
connectionLimit : 100, //important
host : 'localhost',
user : '',
password : '',
database : '',
debug : false
});
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
function handle_database(req, res) {
console.log(req.query.r);
console.log(req.query.id);
pool.getConnection(function (err, connection) {
if (err) {
connection.release();
res.json({ "code" : 100, "status" : "Error in connection database" });
return;
}
console.log('connected as id ' + connection.threadId);
switch (req.query.c) {
case "u":
var post = { ID: req.query.id, VIDEO: req.query.r, Status: req.query.s };
connection.query('INSERT INTO contest SET ?', post, function (err, result) {
connection.release();
if (!err) {
console.log("insert ID "+ result.insertID)
console.log("command " + req.query.c);
console.log("video " + req.query.r);
console.log("ID " + req.query.id);
console.log("Status " + req.query.s);
console.log("Referer " + req.headers.referer);
console.log(o2x({
'?xml version="1.0" encoding="utf-8"?' : null,
Results: {
Inserts: result
}
}));
res.json("help");
}
});
break;
default:
res.json({ "code" : 1000, "status" : "Error command line" });
}
connection.on('error', function (err) {
res.json({ "code" : 100, "status" : "Error in connection database" });
return;
});
});
}
app.get("/", function (req, res) {
-
handle_database(req, res);
});
app.listen(3000);