好吧,我有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]&lt;&lt;这是我想做的一个例子
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的回复
答案 0 :(得分:0)
我认为你在项目中使用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");