【发布时间】:2016-04-01 05:18:07
【问题描述】:
我在尝试接收帖子数据时遇到问题,而无需先从 Web 浏览器运行 html 页面。我想知道是否有一种使用 NodeJS 的方法,有人可以运行给定的 html 文档,只检索 Post 数据和控制台输出。我需要这个才能允许在云或远程服务器环境中进行开发。
当前使用 Server.js :
var spawn = require('child_process').spawn; // Will not do for the situation of development within something similar to an Ubuntu Server
spawn(command, ["http://localhost:80/Develop/Client.html"]);
http.createServer(mainArgs).listen(options); // mainArgs is the function (req,res) callback
function mainArgs(req,res){
if (req.method == 'POST') { // NodeJS is Getting what is posted...
console.log("POST");
var url_parts = url.parse(req.url, true);
currentQuery = url_parts.query;
req.on('end', function () {
console.log("Body: " + currentQuery['stubScript']);
writeHTML(currentQuery['stubScript']);
});
}
..... // HTML, JavaScript, & CSS Handled here
}
当前使用情况 Client.html :
<html>
<head>
//Posts data via XMLHttpRequest()
<script src=devScript.js></script>
</head>
<body>
<script>
// Access Devscript functions and build page using javascript
</script>
</body>
</html>
当前使用 devScript.js:
function postIt(varname, variable) {
var req = new XMLHttpRequest();
var getParms = '?'+ varname + '=' + variable;
req.open('POST', document.URL + getParms, true);
req.onreadystatechange = function(){
if (req.readyState == 4 && req.status == 200)
{
alert('PHPcall() : JSObject =' + varname );
}
};
req.send(getParms);
} // Client is posting...
【问题讨论】:
标签: node.js post get xmlhttprequest