node.js - Sending array via javascript to nodejs -
OK, I have two working scripts that I'm trying to use in conjunction with each other First is my javascript
& lt; Script type = "text / javascript" & gt; Function textareaToArray (t) {return t.value.split (/ [\ n \ r] + /); } Function showArray (msg) {for (i = 0; i & lt; msg.length; i ++) {// some item alerts (msg [i]); } // old code document. GetElementById ("message"). InnerHTML = msg.join ("& amp; # 013;"); } & Lt; / Script & gt; And then I have a node JS script that accepts post data
http://localhost.com/?name=Data The basics where it says
warning (message [i]); I have the postcard in my JavaScript
localhost.com/?name= msg [i] & lt; & Lt; This is an example of what I want to do.
var url = require ('url'); Var http = Required ('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 has given us his name in the GET request, example: http: // host: 8000 /? Name = Tom var basevalue = queryData.name; var value = basevalue.split (" : "); Var exec = requires ('child_process') .exec; console.log (value [0]); exec (" capsurge test.js "+ value [0] +" "+ value [1] + '\ N', function (mistake, stdout, stderr) {response.end ('data sent');});} and {response.end ("contact administrator - not working Is \ n ");}}); // Listen to port 8000, 127.0.0.1 IP defaults on the server .listen (process.env.port); remove my java script o
& lt; Script type = "text / javascript" & gt; Function textareaToArray (t) {return t.value.split (/ [\ n \ r] + /); } ShowArray (msg) {for (i = 0; i & lt; msg.length; i ++) {// some object var data = {}; // Your own data $ Post ("https://new-c9-ashg1990.c9.io" + "?" + $ .perm ({name: msg [i]}), data); Warning (msg [i]); } // old code document. GetElementById ("message"). InnerHTML = msg.join ("& amp; # 013;"); } & Lt; / Script & gt; Can you fix the code given above, so this shows my response from node.js
I think you use jQuery in your project.
Then jQuery provides a good way to do this $. Param This is official example:
var myObject = {a: {one: 1, two: 2, three: 3}, b: [ 1, 2, 3]}; $ MyObject; // result "a% 5 bone% 5d = 1 and a 5% 2% 5d = 2 and a 5% third% 5d = 3 and b% 5b% 5d = 1 and b% 5b% 5 D = 2 and B% 5b% 5D = 3 " In your case, you have to send
function (message) {var message = message || ""; Var url = "SOME_URL"; $ Ajax ({url: url, method: "GET", data: {name: message}, success: function (res) {warning (res);}, error: function (e) {alert (e);}}) ; } ("Some text or message");
Comments
Post a Comment