在使用Google Apps脚本编写Web应用程序多年后,我是使用Azure的新手。我需要改变我的工作方式,以免与iPhone有些不兼容。我使用的是javascript和HTML,所以用node.js重新创建了应用程序。
我能够返回html页面,并运行客户端脚本,但是我需要能够触发服务器端脚本,所以我没有在客户端打开SQL数据库。
server.js脚本如下:
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/html"});
fs.createReadStream(path.resolve(__dirname, 'HTMLPage.html'))
.pipe(res);
}).listen(process.env.PORT || '3000'); // provide a default
function processForm(form) {
var firstName = form.firstName;
var lastName = form.lastName;
console.log(firstName);
}
HTMLPage包含表单代码,以及带有onsubmit命令的按钮,该命令触发客户端脚本,但是我似乎无法触发服务器端脚本(我的尝试以粗体显示)。你能告诉我哪里出错了吗?
<!DOCTYPE html>
<html lang="en">
<script>
function post() {
var first = document.getElementById("firstName").value;
var last = document.getElementById("lastName").value;
window.alert('Thanks, we will contact you soon.');
processForm(form).init; //This is the bit which is not calling the script
};
</script>
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="formdiv">
<form id="form" onsubmit="post();">
<input type="firstName" class="form__input" id="firstName" name="firstName" />
<label class="form__label" for="firstName"><span class="form__label-content">First Name</span></label>
<input type="lastName" class="form__input" id="lastName" name="lastName" />
<label class="form__label" for="lastName"><span class="form__label-content">Last Name</span></label>
<input type="text" id="ddValues" name="ddValues" style="display:none" />
<input type="submit" name="submit" class="btn btn-block" onclick="post();">
</form>
而不是触发脚本,在控制台中我收到此消息:
未捕获的ReferenceError:在HTMLInputElement.onclick上未定义processForm
谁知道我在这里失踪了什么?谢谢
编辑
根据来自ruggierom&naga的信息 - elixir - jar这里是更新的脚本。我的结果现在给了我3个未定义的对象。
Server.js
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/html"});
fs.createReadStream(path.resolve(__dirname, 'HTMLPage.html'))
.pipe(res);
console.log(req.body);
}).listen(process.env.PORT || '3000'); // provide a default
HTML
<head>
<meta charset="utf-8" />
</head>
<body>
<form id="form" name="form" method="post">
<input type="firstName" class="form__input" id="firstName" name="firstName" />
<input type="lastName" class="form__input" id="lastName" name="lastName" />
<input type="submit" name="submit">
</form>
</body>