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 } ); 
4
  • did you check success.status ? Commented Jun 7, 2016 at 5:41
  • @A.T. its return 200, status of redirect request Commented Jun 7, 2016 at 5: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) Commented Sep 30, 2017 at 11:21
  • 1
    @emrass thanks for the thorough explanation, you raise valid points. Reopened. Commented Sep 30, 2017 at 16:03

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's a duplicate, next time better flag.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.