NodeJS-发送简单回复
我尝试发送回chrome浏览器200 OK响应,并且浏览器没有按照我的预期做出反应,他清理屏幕而不是更改特定的'div',这是我的代码:NodeJS-发送简单回复
我的服务器:(node.js的):
function postHandler(request, response) {
request.on('data', function(data) {
body += data;
var parseBody = queryString.parse(body);
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("<div>divi</div>");
});
response.on('end', function() {
console.log("End_Body: " + body);
});
}
和我的JavaScript的浏览器的AJAX调用的样子:
$(document).ready(function() {
$("#register_form").ketchup();
var request;
$("#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();
request = $.ajax({ url: "/welcome.html",
type: "POST",
data: {
'username': username,
'password': password,
'firstname': firstname,
'lastname': lastname,
'age': age
}});
request.done(function (response, textStatus, jqXHR){
$("#server_answer").text("success");
});
request.fail(function (jqXHR, textStatus, errorThrown){
$("#server_answer").text("fail");
});
});
});
什么我做错了???
请节省我一些时间:) 先进的感谢!
我没有执行你的代码,但我的猜测将是你的页面刷新时你的点击处理器结束。
尝试在点击处理程序的末尾添加return false;
。
您可以测试以下更改。在服务器:在浏览器
request.done(function(msg) {
$("#server_answer").text("success");
});
request.fail(function(jqXHR, textStatus) {
$("#server_answer").text("fail");
});
您为done
参数和fail
是在Ajax调用给出的success
和error
处理
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);
});
}
。
从这里采取:http:// stackoverflow。com/questions/5004233/jquery-ajax-post-example – gran33
是的,他们是正确的,你可以检查响应是否到达浏览器或不。查找问题是否在节点或浏览器上。 – user568109
尽量不要发送'数据'上的响应,而是从'结束'... 发送响应,请尝试使用成功方法。
请尝试以下...在服务器:
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
}
请注意,在这里你actuly忽略了身体(我试图保持接近你的代码,因为我可以)...
在客户端:
$(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'
});
请注意,我删除的网址(把“”代替),所以它的默认值是本地主机...你所采取的方式,你actully发送POST请求“/welcome.html”但那不是y的地方OU处理这些请求(可能在server.js什么 - 这是你的本地主机)
我使用了一些this
由于琐Yahav提到的,请尝试以下操作:
statusCode: {
200: function() {
alert('whatever');
}},
,不要忘了..
return false
您确保服务器执行呢?尝试将一些日志放在request.on('data',function(data){,我没有在这里执行代码,因为我没有配置环境,但尝试调试以查看它通过的位置。 –
did you启动服务器,并在什么端口?你有apache服务的原始页面? – VeXii
本地主机:8080,没有apache – gran33