약속과 함께 서버 호출의 동작을 구현하려고합니다.Javascript - 약속 후 리디렉션
서버에서 응답이 "성공"이면 "성공"페이지로 리디렉션하고 "실패"이면 "실패"페이지로 리디렉션하고 싶습니다.
현재 상황은 응답이 대기 중이 아니며 브라우저가 리디렉션 됨 보다 앞에 서버로부터 응답을 받지만 성공 또는 실패 여부는 중요하지 않습니다.
내가 뭘 잘못하고 있으며 어떻게 해결할 수 있습니까?
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.
_ "내가 서버에서 모든 응답 도착하기 전에 브라우저가 리디렉션"'에 "/ 기본"'재인가 _를? '$ location.path ('/ default');의 목적은 무엇입니까? – guest271314
두 번째 콜백은'res' 인수를 사용하지만 현재 인수를받지 않습니다. 실제로'err'이라는 인자를 두 번째 콜백에 추가하고'res' 대신에 그것을 사용해야합니다. 이것들은 인수의 이름 일 뿐이므로 원하는 것을 실제로 사용할 수 있습니다. 그러나, 규칙 상으로,'onRejected()'콜백은'err'이라는 인자를 취해야합니다. – fvgs
응답 전에 확실합니까? 이것이 문제라면 약속은 작동하지 않으며이 코드는 아무 관계가 없습니다. 또한 각도 setTimeout을 사용하는 경우 각도로 동기화되지 않으므로 $ timeout을 사용하려고 할 수 있습니다. –