[英]Pending Type When Redirect From Node + Express
我正在使用JQuery从客户端发送POST请求,并使用node.js重定向到root。 但是在我的开发者控制台中,我得到的状态是:(临时)302,然后键入:Pending
客户代码:
$.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");
});
除非情况发生变化(在过去一年左右的时间里),否则从服务器重定向(使用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';
}
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.