js用post上传数据到服务器,responseText返回错误

我的index.js代码是这样的:
目的是将一个随机数字符串上传到服务器上,然后console.log服务器的返回值。

 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); 

然后我的运行在服务器上的node.js代码名为helloWorld.js是这样的:
目的是接收浏览器传来的随机数,然后返回“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...");
});

当我打开网站的时候:
1.chorme的控制台输出如下:
第一行是我产生的随机数
第二行是console.log("ok:"+xhr.status);的输出
第三行是console.log(xhr.responseText);的输出,问题出在这里,没有返回“ok”,而是显示了服务器上的文件helloWorld.js的代码,这是为什么呢?如何显示正确的结果“ok”?

图片说明

2.云服务器的控制台输出的结果如下:
这里甚至没有输出“????”,这个我也不知道是为什么。
图片说明

请求指教!十分感谢!

是不是被什么防火墙给拦截了所以没有成功

不太懂js怎么拦截请求,请求路径是js文件,在浏览器里面也是返回的js源代码啊,应该是请求路径不对,或者helloword.js并没有拦截到请求。

感觉你是两个服务,第1个是index.js的服务,你的helloWorld是你写的3333的服务。你要想清楚你是不是需要两个服务。
如果不是,要把helloWorld的功能加到第1个服务中去,而不是又创建server。
 //引入模块
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.setHeader('Access-Control-Allow-Origin','*');
        response.write("ok");
        response.end();
});

//设置监听窗口
server.listen(3333,function(){
        console.log("Server running...");
});

log输出:

 ok:200
index.html:11 ok