我正在尝试登录后端服务,该服务在使用表单数据登录后将其重定向到不同的URL。当我尝试使用REST客户端(Postman)登录时,请求是成功的,但是当我尝试使用Angular 2 Ionic尝试相同时。我不确定为什么它不起作用。
Postman的成功请求
General
Request URL:http://127.0.0.1:9000/auth/login
Request Method:POST
Status Code:303 See Other
Remote Address:127.0.0.1:9000
Response Headers
view source
Cache-Control:no-cache, no-store, must-revalidate
Content-Length:0
Date:Thu, 02 Mar 2017 02:55:26 GMT
Expires:0
Location:/auth/onLogin
Pragma:no-cache
Set-Cookie:PLAY_SESSION=e7685926e8ced464e3d4ea8e99b7bc9254170095-pa.u.exp=1489632926758&pa.p.id=password&pa.u.id=test%40example.com; Path=/; HTTPOnly
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Authorization:Basic dGVzdEBleGFtcGxlLmNvbTp0ZXN0MTIzNDU2
Cache-Control:no-cache
Connection:keep-alive
Content-Length:257
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryFfkhCAVy78Bq9fBz
Cookie:PLAY_SESSION=b79f66a47d1a22dd009cc0768fcb8054a2c547c8-pa.u.exp=1489630742877&pa.p.id=password&pa.u.id=test%40example.com
Host:127.0.0.1:9000
Origin:chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
Postman-Token:5cef52b4-dae9-dcde-d79d-d03949cf8a3b
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Request Payload
------WebKitFormBoundaryFfkhCAVy78Bq9fBz
Content-Disposition: form-data; name="email"
test@example.com
------WebKitFormBoundaryFfkhCAVy78Bq9fBz
Content-Disposition: form-data; name="password"
test123456
------WebKitFormBoundaryFfkhCAVy78Bq9fBz--
使用离子2使用Angular 2
代码
public login() {
let headers = new Headers({ 'Content-Type': 'multipart/form-data' });
let options = new RequestOptions({ headers: headers });
var input = new FormData();
input.append('email', 'test@example.com');
input.append('password', 'test123456');
this.http.post('http://127.0.0.1:9000/auth/login', input, headers)
.map((res:Response) => {
var location = res.headers.get('Location');
console.log(res.headers.get('Location'));
}).subscribe(data => {
console.log(data);
});
}
响应
Request URL:
http://127.0.0.1:9000/auth/login
Request Method:
POST
Status Code:
303 See Other
Remote Address:
127.0.0.1:9000
Referrer Policy:
no-referrer-when-downgrade
Response Headersview source
Cache-Control:
no-cache, no-store, must-revalidate
Content-Length:
0
Date:
Fri, 03 Mar 2017 00:38:51 GMT
Expires:
0
Location:
/auth/onLogin
Pragma:
no-cache
Set-Cookie:
PLAY_SESSION=fe2e8c471e089531c12c9cbb0ec9fa346d3cc735-pa.u.exp=1489711131007&pa.p.id=password&pa.u.id=test%40example.com; Path=/; HTTPOnly
Request Headersview source
Accept:
*/*
Accept-Encoding:
gzip, deflate
Accept-Language:
en-US,en;q=0.8
Connection:
keep-alive
Content-Length:
257
Content-Type:
multipart/form-data; boundary=----WebKitFormBoundarysNcuRPNzpXf8OHKP
Host:
127.0.0.1:9000
Origin:
http://localhost:8100
Referer:
http://localhost:8100/
User-Agent:
Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3027.3 Mobile Safari/537.36
Request Payload
------WebKitFormBoundarysNcuRPNzpXf8OHKP
Content-Disposition: form-data; name="email"
test@example.com
------WebKitFormBoundarysNcuRPNzpXf8OHKP
Content-Disposition: form-data; name="password"
test123456
------WebKitFormBoundarysNcuRPNzpXf8OHKP--
并将其重定向到/auth/onlogin
Request URL:
http://127.0.0.1:9000/auth/onLogin
Request Method:
GET
Status Code:
500 Internal Server Error
Remote Address:
127.0.0.1:9000
Referrer Policy:
no-referrer-when-downgrade
Response Headersview source
Content-Length:
7959
Content-Type:
text/html; charset=utf-8
Date:
Fri, 03 Mar 2017 00:38:51 GMT
Request Headersview source
Accept:
*/*
Accept-Encoding:
gzip, deflate, sdch
Accept-Language:
en-US,en;q=0.8
Connection:
keep-alive
Content-Type:
multipart/form-data; boundary=----WebKitFormBoundarysNcuRPNzpXf8OHKP
Host:
127.0.0.1:9000
Origin:
http://localhost:8100
Referer:
http://localhost:8100/
User-Agent:
Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3027.3 Mobile Safari/537.36
您是否尝试在标题后添加withCredentials: true
?
这样:
return this.http. post(
'http://127.0.0.1:9000/auth/login',
{
headers: this.headers,
withCredentials: true
},
)