[英]Setting request cookies angular2 http post request
I have a login service that performs an http request (to a php backend server) and returns a cookie. 我有一个登录服务,执行http请求(到PHP后端服务器)并返回一个cookie。 After login this cookie is needs to be used in all further requests done by the client. 登录后,需要在客户端进行的所有进一步请求中使用此cookie。
loginservice: login服务:
login(userCredentials:UserCredentials):Observable<any> {
this.request.ServiceType = "Connect"
this.request.SessionId = "sessionlessrequest"
this.request.Compression = "no"
this.request.Parameters = userCredentials;
let jsonRequest = JSON.stringify(this.request)
return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
{ headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
})
}).map( (responseData) => {
this.apiResponse = responseData.json() as ApiResponse
if (!this.apiResponse.Error) {
this.sessionData.next(this.apiResponse.Data as UserSessionData)
this.sessionData.asObservable().share
} else {
console.log(this.apiResponse.ErrorMessage)
}
return null
})
When doing this call, the cookie response is like this: 执行此调用时,cookie响应如下所示:
I see that I get the session_id from session_start (on my php server) 我看到我从session_start获取session_id(在我的php服务器上)
I when doing the request : 我在做请求的时候:
searchExams(searchObject:SearchObject):Observable<any>{
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded',)
let options = new RequestOptions({ headers: headers, withCredentials: true});
this.request.ServiceType = ServiceType.SearchExams;
this.request.SessionId = this.userSessionData.SessionId;
this.request.Compression = "no"
this.request.Parameters = searchObject;
let jsonRequest = JSON.stringify(this.request)
console.log(jsonRequest)
return this.http.post("http://localhost/request.php", "data="+ jsonRequest, options
).map( (responseData) => {
this.apiResponse = responseData.json() as ApiResponse
if (!this.apiResponse.Error) {
....
I see that the request cookie is totally different from the one I got from the server. 我看到请求cookie与我从服务器获得的cookie完全不同。 Moreover it is always the same PHPSESSID 而且它总是相同的PHPSESSID
Do I need to set the request cookie? 我需要设置请求cookie吗? How? 怎么样? What am I missing? 我错过了什么?
Thanks.... 谢谢....
Try this for the login: 试试这个登录:
(...)
return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
{ withCredentials: true,
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
})
}).map(...)
And the other requests: 其他要求:
(...)
return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
{withCredentials: true}).map(...)
Basically, just use withCredentials
option set to true
. 基本上,只需使用withCredentials
选项设置为true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.