Javascript NodeJS-发送简单响应

Javascript NodeJS-发送简单响应,javascript,node.js,jquery,Javascript,Node.js,Jquery,我尝试向chrome浏览器发送200 OK响应,但浏览器没有像我预期的那样做出反应,他清理了屏幕,而是更改了特定的“div”,这是我的代码: 我的服务器:(node.js): 我做错了什么?您能用以下更改进行测试吗。在服务器中: function postHandler(request, response) { request.on('data', function(data) { body += data; }); request.on('end', f

我尝试向chrome浏览器发送200 OK响应,但浏览器没有像我预期的那样做出反应,他清理了屏幕,而是更改了特定的“div”,这是我的代码:

我的服务器:(node.js):


我做错了什么?

您能用以下更改进行测试吗。在服务器中:

function postHandler(request, response) {
    request.on('data', function(data) {
        body += data;
    });
    request.on('end', function() {
        var parseBody = queryString.parse(body);

        response.writeHead(200, {"Content-Type": "text/plain"});
                response.end("<div>div</div>");
        console.log("End_Body: " + body);
    });
}

您为
done
fail
提供的参数用于ajax调用中给出的
success
error
处理程序。

尝试不从on“data”发送响应,而是从on“end”发送响应。。。 在客户机中,尝试使用success方法

试试下面的。。。在服务器中:

function postHandler(request, response) {
var body = '';
//when the request comes with some body
request.on('data', function (data) {
        body += data;
        // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB 
        if (body.length > 1e6) { 
            // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST
            request.connection.destroy();
        }
    });

//when the request is finished. (maybe it's data is long and its takes time)
request.on('end', function () {
      console.log("End_Body: " + body);
      response.writeHead(200, { 'Content-Type': 'text/plain'});
      response.write('<div>divi</div>'); 
      response.end();
    });//end of 'end' event
}
请注意,我删除了url(改为放置“”),因此其默认值为localhost。。。您这样做的方式实际上是将post请求发送到“/welcome.html”,但这不是您处理这些请求的地方(可能是在server.js或其他东西上,这是您的本地主机)


我使用了一些

我没有执行你的代码,但我猜当你的点击处理程序结束时,你的页面会刷新


尝试添加
返回false在单击处理程序的末尾。

如Zohar Yahav所述,请尝试以下操作:

statusCode: {
200: function() {
alert('whatever');
}},
别忘了

return false

您确定服务器也会执行吗?尝试在request.on('data',函数(data)中放入一些日志{,我没有在这里执行代码,因为我没有配置环境,但尝试调试以查看它通过的位置。是否启动了服务器以及在哪个端口上?是否有apache为原始页面提供服务?localhost:8080,no ApacheTake:是的,它们是正确的,您是否可以检查响应是否到达浏览器。要查找problem位于节点或浏览器上。
function postHandler(request, response) {
var body = '';
//when the request comes with some body
request.on('data', function (data) {
        body += data;
        // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB 
        if (body.length > 1e6) { 
            // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST
            request.connection.destroy();
        }
    });

//when the request is finished. (maybe it's data is long and its takes time)
request.on('end', function () {
      console.log("End_Body: " + body);
      response.writeHead(200, { 'Content-Type': 'text/plain'});
      response.write('<div>divi</div>'); 
      response.end();
    });//end of 'end' event
}
$(document).ready(function() {
    $("#register_form").ketchup();

    $("#submit_btn").on('click',function() {
        var username = $('#reg_username').val();
        var password = $('#reg_password').val();
        var firstname = $('#firstname').val();
        var lastname = $('#lastname').val();
        var age = $('#age').val();

        $.ajax({ url: "",
                 type: "POST",
                 data: {'username': username,
                        'password': password,
                        'firstname': firstname,
                        'lastname': lastname,
                        'age': age},
                 success: function(data) {//on success
                      $("#server_answer").text("success");
                 },
                 fail: function (jqXHR,textStatus,errorThrown){ // when the ajax call fails
                      $("#server_answer").text("fail");

                 }
        }); //of ajax
    });//of on 'click'
});
statusCode: {
200: function() {
alert('whatever');
}},
return false