使用$ http.post和res.redirect不解决的承诺 [英] Using $http.post and res.redirect without resolving the promise

查看:979
本文介绍了使用$ http.post和res.redirect不解决的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using an $http.post call to my server and sending over some user data:

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

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

My server then performs a series of operations and retrieves a certain id from the DB. I would like to have the client redirected to the page ('streams/'+id), so I have the operations terminate with a

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

我可以通过ID发送:

I could send over the id:

res.json({id:id})

,然后用$位置,或window.open一个.success()子句(如被看见这里 ,或<一个href=\"http://stackoverflow.com/questions/16629057/handle-an-ex$p$pss-redirect-from-angular-post\">here)但似乎有点浪费?服务器已经知道到哪里送我,我为什么要进行额外的电话

and then respond to a .success() clause with a $location, or window.open (as seen here, or here) but that seems a little wasteful: the server already knows where to send me, why should I make an additional call?

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

While the server shows that the routing was done, the new page never renders, it just stays on the original page.

感谢您的帮助!

推荐答案

您的服务器端code不能从Ajax响应重定向浏览器。从服务器端重定向工作没有Ajax的唯一原因是你实际上是每次发送一个新的一页,所以重定向手段只是服务于不同的页面。 Ajax请求不得到担任一个全新的页面作为响应,所以你被卡住在当前之一。你需要去的'浪费'的路线:)返回ID在响应身体,像你提到的使用角度$位置服务重定向。我建议使用一个承诺,一旦解决执行重定向。

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.

这篇关于使用$ http.post和res.redirect不解决的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆