Angular2,如何防止HTTP重定向

Sha*_*yan 5 javascript redirect http angular

我试图通过angular2 Http模拟用户登录.让我们描述下面的情况.
我有一个php应用程序,用户可以登录throw http://sample.com/login.phpurl(一个表单存在用户名和密码输入,用户应填写输入并按提交按钮)如果登录成功,他们重定向到http://sample.com/dashboard.php.
所以我Http用一个表格数据创建一个帖子,里面有"用户名"和"密码".并将Http标头设置content-typeapplication/x-www-form-urlencoded.
登录工作正常,请求自动重定向到http://sample.com/dashboard.php.
问题是我需要访问第一个请求响应头(http://sample.com/login.php),但是Http响应了第二个请求响应头(http://sample.com/dashboard.php).
是否有任何方法可以防止Http重定向?或者在状态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
    }
);
Run Code Online (Sandbox Code Playgroud)

zpu*_*pul 2

这是不可能的,因为 XMLHttpRequest(低级 API)不公开这样的方法。

有关更多详细信息,请查看此:

防止 Xmlhttprequest 重定向