我有一個簡單的HTML這樣的:變量傳給Node.js加載
<html>
<head><title></title></head>
<body>
<script>var testVariable = "Hello"</script>
<form method="post" action="/">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
而且我的Node.js是這樣的:
app.post('/', function(req, res) {
Console.log(req.body.testVariable);
});
我需要做的是當表單提交testVariable
需要傳遞給Node.js函數,我試圖隱藏字段,但我仍然有問題,即:
<input type="hidden" name="chat" value="<script>testVariable</script>">
但是,你可以想象它將所有腳本作爲字符串傳遞,而不是變量的值。
有人知道該怎麼做嗎?對不起,如果這是一個愚蠢的問題,我通常是JavaScript和Node.js的新手,我無法在Google中找到答案。
謝謝。
-----編輯------
我的形式,現在看起來是這樣的:
<form method="post" action="/">
<input type="hidden" name="chat" id="hiddenInput" />
<script>
var input = document.getElementById('hiddenInput');
input.value = $('#conversation');
</script>
<input type="submit" name="submit" value="Submit">
</form>
而且在我的Node.js我打印這樣的對象:
console.log(JSON.stringify(req.body.chat));
並且它打印"[object Object]"
(包括引號)。
我覈實所收到的變量是一個字符串:
console.log(typeof req.body.chat); // prints "string"
將數據發佈爲基於輸入元素的name屬性命名的變量,其值由元素值屬性定義。 – 2014-12-01 22:08:55