简体   繁体   English

从节点+ Express重定向时的挂起类型

[英]Pending Type When Redirect From Node + Express

I am sending POST request from client using JQuery and redirecting to root using node.js. 我正在使用JQuery从客户端发送POST请求,并使用node.js重定向到root。 But in my developer console i am getting the status : 302 (temporarily) and type : Pending 但是在我的开发者控制台中,我得到的状态是:(临时)302,然后键入:Pending

Client Code: 客户代码:

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

Server Code: 服务器代码:

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");
    });

Unless things have changed (over the last year or so), redirect from the server (using expressJS) will not work for a POST operation from the client. 除非情况发生变化(在过去一年左右的时间里),否则从服务器重定向(使用expressJS)将不适用于来自客户端的POST操作。 You can redirect from the client side using something like this (essentially, when you need to redirect, send a certain message that you can check on in the callback handler for the post operation): 您可以使用以下方式从客户端进行重定向(基本上,当您需要重定向时,发送某些消息,您可以在回调处理程序中检查该消息以进行发布操作):

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

In your case, it will be this part: 在您的情况下,将是以下部分:

    success: function(data){ 
 window.location = 'page to redirect to';
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM