Redirect page after post request [Express 4]

后端 未结 1 318
日久生厌
日久生厌 2020-12-17 04:30
/**
 * @api {post} /logout Logout from system
 * @apiName Logout
 * @apiGroup Login
 */
router.post(\"/logout\", function (req, res) {
  req.logout();
  req.session.         


        
相关标签:
1条回答
  • 2020-12-17 05:19

    There are no redirects from a Javascript generated Ajax call which your $.post() is. Ajax sends a request, gets a response. An ajax call by itself does not change the page location at all. That's a characteristic of Ajax calls.

    Redirects work when the browser is loading a new page and the server tells it to change what page it is loading to a new source, not when the browser just sends an Ajax call.

    You can, of course, use your client-side Javascript to decide to redirect from the client side after your $.post() finishes. You could even have the response from the $.post() be the new location and your client-side Javascript could then just set window.location to that new URL.

    function logOut() {
        $.post("/logout").then(function(data) {
            window.location = data.redirectUrl;
        });
    }
    

    And, on the server:

    router.post("/logout", function (req, res) {
      req.logout();
      req.session.destroy();
      res.send({err: 0, redirectUrl: "/"});
    });
    
    0 讨论(0)
提交回复
热议问题