Angular2 , How to prevent from HTTP redirection

2019-05-01 03:48发布

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
    }
);

1条回答
走好不送
2楼-- · 2019-05-01 04:28

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

查看更多
登录 后发表回答