Angular 4 Reponse Headers未设置所有值

时间:2017-07-20 04:23:03

标签: angular

在angular 4应用程序中,我通过CORS Web服务接收HTTP响应。但是角度4 (版本4.3.0)响应对象只是在Promise.then(响应)响应对象中设置内容类型并忽略其他对象。

以下是我在Chrome Developer Tool网络标签中收到的回复标题。

Access-Control-Allow-Headers:Content-Type, Accept, Origin, Authorization

Access-Control-Allow-Methods:POST, GET, PUT, DELETE, OPTIONS

Access-Control-Allow-Origin:*

Connection:Keep-Alive

Content-Encoding:gzip

Content-Length:20

Content-Type:application/json;charset=utf-8

Date:Thu, 20 Jul 2017 04:08:10 GMT

Keep-Alive:timeout=3, max=26

Authorization:xyzpqrmnoabc

Vary:Authorization,Accept-Encoding

X-Powered-By:Servlet/3.1 JSP/2.3 (Tomcat/8.5 JRE/1.7)

这是从Response对象中获取的Headers对象,它只包含并缺少其他标题。

{"Content-Type":["application/json;charset=utf-8"]}

是否由于Promise,响应标头设置不正确? 我是否需要转移到Observables?

postData(data: any): Promise<object> {
                return this.http
                    .post(this.url, data, this.options)
                    .toPromise()
                    .then((response) => { return this.handleSuccess(response); })
                    .catch((exce) => { return this.handleError(exce); });
            }

handleSuccess(res: Response): Promise<Array<any>> {
            console.log('RESPONSE : ',res); // not contain full headers
            let hdrs: Headers = new Headers(res['headers']); // not contain full headers
            console.log('*** HEADERS', JSON.stringify(hdrs.toJSON())); // not contain full headers
            return Promise.resolve(res.json());
    }

1 个答案:

答案 0 :(得分:0)

尝试此观察并查看结果 -

这是以下功能:

postData(data: any){
    return this.http.post(this.url, data, this.options);                
}

也许你也可以尝试这种承诺方式:

这是以下功能:

postData(data: any){
    return this.http.post(this.url, data, this.options).toPromise();                
}

在代码中的某处使用它:

this.postData(data).subscribe((res)=>{
   console.log(res); 
   // This should have all headers along with statusCode and status
})

或承诺

this.postData(data).then((res)=>{
       console.log(res); 
       // This should have all headers along with statusCode and status
    }, (err)=>{
       console.log(err); 
       // This should have all headers along with statusCode and status
    })