Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 , How to prevent from HTTP redirection

I am trying to simulating user login by angular2 Http. let describe situation as bellow.
i have a php application that users can login throw http://sample.com/login.php url (a form exist with username and password input, user should fill inputs and press submit button) and if login is success, they redirect to http://sample.com/dashboard.php.
so i create a Http post with a form data that has "username" and "password" within. and set Http header content-type as application/x-www-form-urlencoded.
login works fine and request automatically redirect to http://sample.com/dashboard.php .
problem is that i need to access first request response header(http://sample.com/login.php), but the Http response me the second request response header (http://sample.com/dashboard.php).
is that any way to prevent Http from redirecting? or throw error on status 302 ?

var headers = new Headers({'Content-Type':'application/x-www-form-urlencoded'});
var options = new RequestOptions({ headers: headers });
Http.post("http://sample.com/login.php",'username=admin&password=123' , options)
    .subscribe(success => {
        // success and return redirected response
        // response of http://sample.com/dashboard.php
    }, error => {
        // error handler
    }
);
like image 651
Shayan Avatar asked Jun 07 '16 05:06

Shayan


1 Answers

This is not possible because XMLHttpRequest (low level API) does not expose such a method.

For more details look at this:

Prevent redirection of Xmlhttprequest

like image 134
zpul Avatar answered Oct 13 '22 19:10

zpul