使用$ http.post和res.redirect没有解决的承诺(Using $http.post

2019-10-18 19:57发布

我使用的是$ http.post调用我的服务器并发送了一些用户的数据:

$http.post('/streamdb/create/',{user:$scope.user.username})

我的服务器然后执行一系列操作,并检索从DB一定的ID。 我想已在客户端重定向到该页面(“流/” + id)的,所以我的操作与终止

res.redirect('/streams/'+id)

我可以通过ID发送:

res.json({id:id})

然后响应以$位置,或window.open一个.success()子句(如看到这里 ,或这里 ),但似乎有点浪费:服务器已经知道在哪里送我,我为什么要进行额外的呼叫?

当服务器显示路由做,新的页面渲染从来没有,它只是停留在原来的页面上。

谢谢你的帮助!

Answer 1:

Your server side code cannot redirect the browser from an ajax response. The only reason that redirecting from the server side works without ajax is you are actually sending a fresh page each time, so a redirect means simply serving a different page. Ajax requests dont get served an entire new page as a response, so you are stuck on the current one. You need to go the 'wasteful' route :) Return the id in the response body and redirect using the angular $location service like you mentioned. I recommend using a promise and performing the redirect once it resolves.



文章来源: Using $http.post and res.redirect without resolving the promise