I am new to Node.js and this is my first project with it. I created a Node.js file called test.js. It has an array, say a.
Now I want to create an HTML file that calls this test.js on a button click event. Then get the data from this file and publish it in a table in the HTML file.
I already wrote a Node.js file and I can see the results on console.log (a). But I cannot figure out how to send this array in HTML when it asks for it.
Meanwhile, I googled and compiled some code. The request arrives at the server, but I always get an error message from the server. Why is that?
Client side -
function fetch() { $.ajax({ type: 'POST', url: "http://127.0.0.1:8888", data: 'China', datatype: 'json', success: function (data) { alert("hi"); var ret = jQuery.parseJSON(data); $('#q').html(ret.msg); }, error: function (xhr, status, error) { alert("hii"); } });
Server side:
http.createServer(function(request, response) { console.log("Request received"); response.writeHeader(200, {"Content-Type": "application/json"}); request.on('data', function (chunk) { console.log(chunk.toString('utf8')); consol.log(result); response.write(JSON.stringify({data : result})); }); response.end(); }).listen(8888);
I can see China on the console. But I do not return an array of results back to the client. The result is an array, and I get its value on the console. I just wonβt return it to the client. Any help?
Wayne rooney
source share