我正在使用一個基本的node.js介紹腳本,並且我在命令行中傳遞了一個參數。 我希望這個參數在運行後傳回客戶端。我可以傳回任何純文本或值的東西,但我無法傳回包含相同信息的變量。節點JS返回undefined?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>get test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js">
</script>
</head>
<body>
<h1>Get Test</h1>
<div id="test"></div>
<script>
$(document).ready(function() {
$.ajax({url: 'http://localhost',dataType: "jsonp",jsonpCallback: "_testcb",cache: false,timeout: 5000,success: function(data) {
$("#test").append(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
});
});
</script>
</body>
</html>
這是我的服務器代碼:
var http = require('http');
var arr = new Array();
http.createServer(function (req, res) {
res.writeHead(200);
// print process.argv
process.argv.forEach(function(val, index, array) {
console.log(index + ': ' + val);
arr[index] = val;
res.write("Size" + val);
res.end('_testcb(\'{"Size: \' + val + \'"}\')');
});
console.log(arr[2]);
}).listen(80);
所以我的問題是,我怎麼傳回的對象/變量/數組而不是靜態的文字?
謝謝,這是有道理的,並感謝深入的解釋。 – Phil