我使用JQuery从客户端发送POST请求,并使用node.js重定向到root。 但在我的开发人员控制台中,我获得状态:302(暂时)并输入:待定
客户代码:
$.ajax({
type: "POST",
dataType: "json",
data: JSON.stringify({
username_var:username_var,
password_var:password_var
}),
url: "/login",
success: function(data){
alert(data)
console.log(data);
alert(data.name);
}
});
服务器代码:
if (req.method == 'POST'){
console.log("[200] " + req.method + " to " + req.url);
req.on('data', function(chunk) {
console.log("Received body data:");
console.log(chunk.toString());
var userLoginObject = JSON.parse(chunk);
console.log(userLoginObject.username_var);
console.log(userLoginObject.password_var);
var status = {
name: "CHATEAU DE SAINT COSME",
year: "2009",
grapes: "Grenache / Syrah",
country: "France",
region: "Southern Rhone",
description: "The aromas of fruit and spice...",
picture: "saint_cosme.jpg"
};
res.redirect("/");
res.end();
// //res.send(status);
// res.writeHead(301, {"Location": "http://localhost:8000/"});
// res.end("Test");
});
答案 0 :(得分:0)
除非事情发生变化(过去一年左右),否则从服务器重定向(使用expressJS)将不适用于客户端的POST操作。您可以使用类似的方式从客户端重定向(实际上,当您需要重定向时,发送一个可以在回调处理程序中检查的特定消息以进行后期操作):
$.post('/login',function(){ //window.location to be called upon message from server
window.location = 'page to redirect to'; //you can check for a certain message from server
});
在你的情况下,它将是这一部分:
success: function(data){
window.location = 'page to redirect to';
}
希望它有所帮助。