最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 , How to prevent from HTTP redirection - Stack Overflow

programmeradmin2浏览0评论

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 .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 .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 .php .
problem is that i need to access first request response header(.php), but the Http response me the second request response header (.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(".php",'username=admin&password=123' , options)
    .subscribe(success => {
        // success and return redirected response
        // response of .php
    }, error => {
        // error handler
    }
);

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./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./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./dashboard.php .
problem is that i need to access first request response header(http://sample./login.php), but the Http response me the second request response header (http://sample./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./login.php",'username=admin&password=123' , options)
    .subscribe(success => {
        // success and return redirected response
        // response of http://sample./dashboard.php
    }, error => {
        // error handler
    }
);
Share Improve this question edited Jun 7, 2016 at 5:33 Shayan asked Jun 7, 2016 at 5:28 ShayanShayan 9669 silver badges20 bronze badges 4
  • did you check success.status ? – A.T. Commented Jun 7, 2016 at 5:41
  • @A.T. its return 200, status of redirect request – Shayan 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) – emrass Commented Sep 30, 2017 at 11:21
  • 1 @emrass thanks for the thorough explanation, you raise valid points. Reopened. – user447356 Commented Sep 30, 2017 at 16:03
Add a ment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论