Angular2,如何防止HTTP重定向 [英] Angular2 , How to prevent from HTTP redirection
问题描述
我正在尝试通过angular2 Http来模拟用户登录。让我们来描述一下情况。
i有一个php应用程序,用户可以登录 http://sample.com/login.php
url(a表单存在用户名和密码输入,用户应填写输入并按提交按钮)如果登录成功,他们将重定向到 http://sample.com/dashboard.php
。
所以我创建一个 Http
帖子,其中包含一个表格数据,其中包含用户名和密码。
并将Http标头内容类型
设置为 application / x-www-form-urlencoded
。
登录正常,请求自动重定向到 http://sample.com/dashboard.php
。
问题我需要访问第一个请求响应标头( http://sample.com/login.php ),但Http会回复第二个请求响应标头( http://sample.com /dashboard.php )。
是否可以防止Http重定向?或者在状态302上抛出错误?
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
}
);
推荐答案
这是不可能的,因为XMLHttpRequest(低级API)没有公开这样的方法。
This is not possible because XMLHttpRequest (low level API) does not expose such a method.
详情请看:
这篇关于Angular2,如何防止HTTP重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!