I'm starting learning node.js, but I have a little problem. I have an ajax function that call a server listening on port 8001. Afer I open and load a .json file and I want to send back to the page. For what concern the loading, there is no problem, but when I send back to the page, I get this error : on Chrome "POST http: //localhost:8001/ net::ERR_CONNECTION_REFUSED"; on Firefox "...Reason : CORS request failed."
here the code of my simple file .js :
var http = require('http'),
fs = require('fs'),
url = require('url');
http.createServer(function (request, response) {
var path = url.parse(request.url).pathname;
console.log("request recieved");
var obj;
fs.readFile('../menu.json', 'utf8', function (err, data) {
if (err) throw err;
console.log(data);
obj = JSON.parse(data);
console.log(obj);
response.writeHead(200, { "Content-Type": "application/json" });
response.end(obj, "utf-8");
});
}).listen(8001);
console.log("server initialized !!");
How can I make it works? I also read the other topic, but I have not found a solution. Thank you in advance for your time and patience.
Edit : here is the ajax call :
function testNodeJs() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://localhost:8001/", true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var string = xmlhttp.responseText;
alert(string);
}
}
xmlhttp.send();
};