我想通过angular2 Http模拟用户登录。让情况描述如下。
我有一个PHP应用程序,用户可以登录抛出http://sample.com/login.php
URL(一个表单存在与用户名和密码输入,用户应填写输入,并按提交按钮),如果登录成功,他们重定向到http://sample.com/dashboard.php
。
所以我创建了一个Http
后的表单数据,其中有“用户名”和“密码”。 并将Http标头content-type
设置为application/x-www-form-urlencoded
。
登录工作正常,请求自动重定向到http://sample.com/dashboard.php
。
问题是我需要访问第一个请求响应头(http://sample.com/login.php),但Http响应我第二个请求响应头(http://sample.com/dashboard.php)。
是否有阻止Http重定向的方法?或在状态302上抛出错误?Angular2,如何防止HTTP重定向
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
}
);
你检查success.status? –
@ A.T。其返回200,重定向请求的状态 – Shayan
@Shadow Wizard我不认为这是一个*确切*重复。这个问题是针对Angular 2的,因此将其标记为重复将意味着每个人都在这个问题上磕磕绊绊的确知道Angular 2的HTTP服务在底层使用了XMLHttpRequest。我非常怀疑这一点 - 否则这个问题的> 1000个观众可能直接搜索HXMLHttpRequest,并且从来没有登陆过。 IMO的正确答案是Angular的HTTP服务使用XMLHttpRequest(连同你标记为重复的问题的链接) – emrass