等待从Node + Expressredirect的挂起types

我使用JQuery从客户端发送POST请求,并使用node.jsredirect到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"); }); 

除非事情发生了变化(在过去的一年左右),从服务器redirect(使用expressJS)将不适用于来自客户端的POST操作。 你可以使用像这样的东西从客户端redirect(实质上,当你需要redirect,发送一个特定的消息,你可以在callback处理程序中检查后操作):

 $.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'; } 

希望能帮助到你。