本文介绍了设置请求cookie angular2 http post请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录服务,执行http请求(到php后端服务器)并返回一个cookie。登录后,需要在客户端完成的所有其他请求中使用此cookie。

I have a login service that performs an http request (to a php backend server) and returns a cookie. After login this cookie is needs to be used in all further requests done by the client.

loginservice:

loginservice:

   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
        })

执行此调用时,cookie响应如下:

When doing this call, the cookie response is like this:

我看到我从session_start获取session_id(在我的php服务器上)

I see that I get the session_id from session_start (on my php server)

我在做请求时:

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) {
....

我看到请求cookie与我的完全不同从服务器获得。此外它总是相同的PHPSESSID

I see that the request cookie is totally different from the one I got from the server. Moreover it is always the same PHPSESSID

我是否需要设置请求cookie?如何?
我缺少什么?

Do I need to set the request cookie? How?What am I missing?

谢谢....

推荐答案

尝试登录:

(...)
return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
    { withCredentials: true,
        headers: new Headers({
        'Content-Type': 'application/x-www-form-urlencoded'
    })
    }).map(...)

以及其他要求:

(...)
return this.http.post("http://localhost/request.php", "data="+ jsonRequest,
                      {withCredentials: true}).map(...)

基本上,只需使用 withCredentials 设置为 true

Basically, just use withCredentials option set to true.

这篇关于设置请求cookie angular2 http post请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:47