簡體   English   中英

從節點+ Express重定向時的掛起類型

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM