my index.js code goes like this:
aims to upload a random number string to the server, and then console.log the server"s return value.
var randomNum = Math.floor(Math.random() * 5).toString();
console.log(randomNum);
var xhr = new XMLHttpRequest(); //XHR
xhr.open("post","helloWorld.js");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
console.log("ok:"+xhr.status);
console.log(xhr.responseText);
}else {
alert("Request was unsuccessful:" + xhr.status);
}
} //if
};
xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
xhr.send(randomNum);
then my node.js code running on the server, called helloWorld.js, goes like this:
aims to receive random numbers from the browser and then return "ok"
//
var http = require("http");
var url = require("url");
var querystring = require("querystring");
console.log("!!!");
//
var server = http.createServer( function(request, response) {
console.log("?????");
console.log(request);
var getData = "";
request.on("data", function(chunk){ getData += chunk;});
console.log(getData);
//response.writeHeader(200,{"Content-Type":"text/javascript;charset=UTF-8"});
response.write("ok");
response.end();
});
//
server.listen(3333,function(){
console.log("Server running...");
});
when I open the website: the console output of
1.chorme is as follows:
the first line is the random number I generated
the second line is console.log ("ok:" + xhr.status); the third line of the output
is console.log (xhr.responseText); the problem lies here, instead of returning "ok", it shows the code of the file helloWorld.js on the server, which is why? How to display the correct result "ok"?
2.:
""
ask for advice! Thank you very much!