好吧,我有2个工作脚本,首先我要与彼此一起使用,这是我的javascript
<script type="text/javascript">
function textareaToArray(t){
return t.value.split(/[\n\r]+/);
}
function showArray(msg){
for(i = 0; i < msg.length; i++) {
// something per item
alert(msg[i]);
}
// the old code
document.getElementById("message").innerHTML = msg.join("
");
}
</script>
然后我有一个节点js脚本,可以接受postdata
http://localhost.com/?name=Data
基本上说
alert(msg[i]);
在我的JavaScript中,我想将数据发布到
localhost.com/?name= msg [i] <
var url = require('url');
var http = require('http');
var server = http.createServer(function (request, response) {
var queryData = url.parse(request.url, true).query;
response.writeHead(200, {"Content-Type": "text/plain"});
if (queryData.name) {
// user told us their name in the GET request, ex: http://host:8000/?name=Tom
var basevalue = queryData.name;
var value = basevalue.split (":");
var exec = require('child_process').exec;
console.log(value[0]);
exec ("casperjs test.js " + value[0] + " " + value[1] + '\n',function(err, stdout, stderr) {
response.end('data sent');
});
} else {
response.end("Contact Admin - Not Working\n");
}
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(process.env.PORT);
我的Java脚本
<script type="text/javascript">
function textareaToArray(t){
return t.value.split(/[\n\r]+/);
}
function showArray(msg){
for(i = 0; i < msg.length; i++) {
// something per item
var data = {}; //your own data
$.post("https://new-c9-ashg1990.c9.io" + "?" + $.param({name: msg[i]}), data);
alert(msg[i]);
}
// the old code
document.getElementById("message").innerHTML = msg.join("
");
}
</script>
您能修复我的上述代码,以便显示来自node.js的响应吗
最佳答案
我认为您在项目中使用jQuery。
因此,jQuery提供了执行此操作的好方法$ .param
这是官方示例:
var myObject = {
a: {
one: 1,
two: 2,
three: 3
},
b: [ 1, 2, 3 ]
};
$.param(myObject);
// result is "a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B%5D=2&b%5B%5D=3"
就你而言
function send(message){
var message = message || "";
var url = "SOME_URL";
$.ajax({
url: url,
method: "GET",
data: { name: message },
success: function(res){
alert(res);
},
error: function(e){
alert(e);
}
});
}
send("some text or message");
关于javascript - 通过JavaScript将数组发送到Node.js,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21826875/