8

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
    }
);
Shayan
  • 956
  • 9
  • 20
  • did you check success.status ? – A.T. Jun 07 '16 at 05:41
  • @A.T. its return 200, status of redirect request – Shayan Jun 07 '16 at 05:43
  • 1
    @Shadow Wizard I would not consider this an *exact* duplicate. This question is specific to Angular 2, so marking this as a duplicate would imply that everyone stumbling upon this question do know that Angular 2's HTTP service uses XMLHttpRequest under the hood. I highly doubt this - otherwise the >1000 viewers of this question could have searched for HXMLHttpRequest directly and would have never landed here. IMO the right answer is that Angular's HTTP service uses XMLHttpRequest (along with a link to the question you marked as duplicate) – emrass Sep 30 '17 at 11:21
  • 1
    @emrass thanks for the thorough explanation, you raise valid points. Reopened. – Shadow The GPT Wizard Sep 30 '17 at 16:03

1 Answers1

2

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

zpul
  • 1,535
  • 1
  • 14
  • 19