2017-07-20 7 views
0

角度4アプリケーションでは、CORS Webサービス経由でHTTP応答を受信して​​います。しかし、角度4 (バージョン4.3.0)応答オブジェクトはコンテンツタイプを設定し、Promise.then(応答)応答オブジェクト内の他を無視するだけです。角度4応答ヘッダーがすべての値を設定していません

Chromeデベロッパーツールの[ネットワーク]タブで受け取ったレスポンスヘッダーは次のとおりです。

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) 

他のヘッダーが含まれていて、欠落しているレスポンスオブジェクトから取得したヘッダーオブジェクトです。

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

それが原因の約束であり、レスポンスヘッダが正しく設定されていませんか? 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()); 
    } 
+0

のための完全なヘッダは、あなたのレスポンスオブジェクト内のオブジェクトは何?あなたのHTTPトリガーコードは何ですか?クロムの開発ツールの – Gary

+0

私は完全な応答以上に受け取ります。私の応答オブジェクトのヘッダーには、Content-Type –

+0

Okが含まれています。 Http.get/postなどのオブザーバブルを試してみてください。これはヘッダー仕様です。何もそれほど何もありません。https://fetch.spec.whatwg.org/#headers-class。できない場合でもHTTPメソッドとトリガーを表示できますか?オブザーバブルからヘッダーを取得するには? – Gary

答えて

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

これを試しましたが、** import {Http} from '@ angular/http' ** ** import {HttpClient}を実装します'@ angular/common/http'; **その後、私はチェックするでしょう。私はなぜhttpの2実装があるのか​​分かりません。 –

+0

hmm ...あなたはどのバージョンのngを使用していますか?それは4.3.1と私のために働く。 APIの古いバージョンは、常に2つのバージョンサイクルで角度があります。 – Gary

関連する問題