How to redirect to another domain from $http.post success callback?

37 views
Skip to first unread message

James Drinkard

unread,
Aug 4, 2017, 5:37:19 PM8/4/17
to Angular and AngularJS discussion
I'm using Angularjs 1.4.2 and I need to redirect to another URL after $http.post success callback.  I know I can use something like: $window.location.href = "http://google.com", but in this case
I think need to call a function in the service from the custom directive to get the URL dynamically.  

//custom directive
$scope.login = function () {
var loginResponse = {};
var errorMessage = "";
var access_token = "";
loginService.login(myInfo).then(function(response) {
console.log("response is: " + JSON.stringify(response));
if(response.data.token) {
$window.sessionStorage.setItem("token", response.data.token);
//Do I make the call from here?
$window.location.href = loginService.getURL();
}else{
errorMessage = response.data.errorMessage.split(":").pop();
console.log("errorMessage is: " + errorMessage);
... more code
So, is this a bad practice the way I'm trying to implement it?

Thank you,
James

Sander Elias

unread,
Aug 5, 2017, 1:44:23 AM8/5/17
to Angular and AngularJS discussion
Hi James,

Well, it's definitively not a common thing to do. However, sometimes you need do to stuff like this. I know I have similar code, that deals with payment providers. If you are redirecting to another page in your own app, this is not the way to go. 

Regards
Sander

James Drinkard

unread,
Aug 8, 2017, 1:47:14 PM8/8/17
to Angular and AngularJS discussion


Okay, well it seemed different to me, but it is in the requirement.

Thank you,
James
Reply all
Reply to author
Forward
Message has been deleted
0 new messages