0

I do a post ajax request to node with koa-router , and I want to redirect '/' ,but url didn`t change . this is my code :

router.post('/action',function *(next){
     //some action
     this.redirect('/');
});

my wish is '/login' = > '/' ,but url didn`t change , '/login' => '/login' . this is the result

xiao xin
  • 271
  • 1
  • 3
  • 3

2 Answers2

1

You can't redirect a browser via an ajax response. The browser executes ajax requests asynchronously and a Location: ... header in an ajax response will not change the browser URL. The ajax request itself is being redirected to the / url, but not your current browser window.

jbielick
  • 2,800
  • 17
  • 28
0

@jbielick is on the right path: I cannot think on any scenario that makes sense to do a redirection in an ajax request.

Anyway, forgetting the ajax issue. there's an easier way to do redirections with koa-router

Simply use:

 
  router.redirect('/action', '/');
mtsdev
  • 613
  • 5
  • 11