Javascript -在承诺后重定向
Javascript - Redirect after promise
我正在尝试实现带有承诺的服务器调用行为。
我想要发生的是,当来自服务器的回复是"成功"时,我想重定向到"成功"页面,否则如果是"失败",我想重定向到"失败"页面。
当前的情况是没有等待响应,并且在我从服务器获得任何响应之前,浏览器被重定向到,无论它是成功还是失败。
我做错了什么,我该如何解决?
下面是我代码的相关部分:
var ch_item;
Payment.postInvoice(dataObj, $rootScope.usertoken).then(function(res){
if (res.message === "success") {
setTimeout(function () {
var payment_details = res.data;
User.getUserById(res.data.stylist_uuid, res.data.customer_uuid).then(function (stylist) {
ch_item = TC_Service.getChatCredentials(stylistInfo.uid, $rootScope.userId, $rootScope.usertoken, dataObj.request_uuid);
openModelAlert("Thank you for your booking!");
$location.path('/success');
});
}, 500);
} else {
setTimeout(function () {
openModal("Sorry, please come back later.");
$location.path('/failure');
}, 500);
}
$modalInstance.dismiss('cancel');
$location.path('/default');
}, function () {
console.log("Something went wrong with the payment, the response is: "+res.message);
});
帮帮我,伙计们:)
感谢您的宝贵时间。
v .
当返回承诺时,您不需要放入if/else语句来确定调用是否失败或成功。这是默认回调的一部分。
.then(
function(response){
//if success show the response in the log
console.log(response)
},function(error){
//if error show the error message in the log
console.log('Error: ' + error.statusText);
}
);
从上面的例子可以看出。第一个回调函数处理成功,第二个回调函数处理错误。我希望你能处理好这件事。
编辑试着这样修改你的代码:
var ch_item;
Payment.postInvoice(dataObj, $rootScope.usertoken).then(function(res){
var payment_details = res.data;
User.getUserById(res.data.stylist_uuid, res.data.customer_uuid).then(function (stylist) {
ch_item = TC_Service.getChatCredentials(stylistInfo.uid, $rootScope.userId, $rootScope.usertoken, dataObj.request_uuid);
openModelAlert("Thank you for your booking!");
$location.path('/success');
});
}
},
function (err) {
console.log("Something went wrong with the payment, the response is: "+err.message);
openModal("Sorry, please come back later.");
$location.path('/failure');
});
相关文章:
- 通过javascript重定向html传递php变量
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 使用angular重定向到html页面
- 如何在chrome扩展中重定向到html页面
- 无法从jquery Mobile导航栏重定向到另一个页面
- ng视图外的链接重定向到ng视图内的页面
- 在phonegap应用程序内部重定向不起作用
- CORS-重定向到第二个GET正在接收的页面
- AJAX不会在文件上传后重定向到网页-POST方法
- 重定向时角度刷新浏览器
- 通过链接重定向不;我不在jstree中工作
- 根据select选项元素将表单重定向到不同的URL
- 通过JQuery重定向到另一个页面
- 重定向到“父窗口”
- 重定向iFrame中的父URL
- PHP-如何重定向到同一页面并更改DOM's
- chrome.tabs.update() 重定向到 'chrome-extension://invalid/'
- AngularJS决心承诺.如果承诺被拒绝,则重定向
- 路由器:从canActivate方法返回的承诺中进行重定向
- Javascript -在承诺后重定向