geek_c
geek_c

Reputation: 31

How to redirect to a new page after a successful POST API call in Jquery

I have the following code in my js file.

    $scope.addLookupAction = function () {
        $("#importForm").attr("action", 'xyz.com');

        success:function(response) {
            $log.info("Redirecting to lookup home page");
            window.location.href='abc.com';
        }
    };

I am trying to redirect the application to a new page(abc.com) on receiving a good http response(200) from 'xyz.com'. Any pointers on why this does not work? I have a bit of angularjs and jquery together which makes it even more convoluted...

Also, this is my first post on stack overflow... So Hello everyone !!! :)

Upvotes: 1

Views: 1152

Answers (4)

Gautam Patadiya
Gautam Patadiya

Reputation: 1412

You can try this working for me..

e.preventDefault();
$http.post('xyz.com').then(function () { 
    window.location = jQuery('.btn').attr('href');
});

Upvotes: 0

Vlad
Vlad

Reputation: 256

Inject $location to your controller:

$location.path('http://exmaple.com').replace()

Upvotes: 0

Rodrigo Kiguti
Rodrigo Kiguti

Reputation: 542

Is the function called when you click a button?

First you need to inject the $location and $http.

$scope.addLookupAction = function (e) {
    e.preventDefault();
    $http.post('xyz.com').then(function () { 
        $location.href = 'abc.com';
    });
};

Upvotes: 1

Pit
Pit

Reputation: 1488

You don't need the href, but should add http://;

$scope.addLookupAction = function () {
    $("#importForm").attr("action", 'xyz.com');

    success:function(response) {
        $log.info("Redirecting to lookup home page");
        window.location.='http://example.com';
    }
};

Upvotes: 0

Related Questions