2017-10-24 17 views
1

投稿要求を投稿しようとしていますが、HttpClientで、残りのクライアントが適切な応答を返すのと同じエラーが表示されます。角度1と同じ$httpサービスが期待どおりに動作しています。HttpClientを使用してHttpClientでポストリクエストを行うことができません。

複数の方法を試しましたが、投稿方法も取得方法も機能していません。最後に:私はproxy.config.json

{ 
    "/api/*":{ 

     "target":"http://10.104.40.14:8290/my_app", 
     "secure":false, 
     "logLevel":"debug" 
    } 
} 

//エラーコードここで

zone.js:2933 POST http://localhost:4200/api/security/login 401 (Unauthorized) 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> 
    <title>Error 401 Unauthorized</title> 
    </head> 
    <body><h2>HTTP ERROR 401</h2> 
    <p>Problem accessing /de_prp/error. Reason: 
    <pre> Unauthorized</pre></p> 
    </body> 
    </html> 

を設定している角度-CLIを使用しています は私auth.service.tsが

import { Injectable, OnInit } from '@angular/core'; 

    import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http'; 
     import { RequestOptions, Headers } from '@angular/http'; 
     import { URLSearchParams } from '@angular/http'; 
     @Injectable() 
     export class AuthService implements OnInit { 
      constructor(private http: HttpClient) { } 

      ngOnInit(): void { 

      } 


      login(username, password, rememberMe) { 
      console.log(username, password, rememberMe); 

      //const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8'); 
      const body = JSON.stringify({ username: username, password: password }); 
      const headers = new HttpHeaders({ 'Content-Type': 'application/json' }); 
      this.http.post("/api/security/login", body, { headers: headers }).subscribe(
       res => { 
       console.log(res); 
       }, 
       (err: HttpErrorResponse) => { 

       console.log(err.error); 
       console.log(err.name); 
       console.log(err.message); 
       console.log(err.status); 
       } 
      ) 
      } 
      logout() { 

      this.http.get("/api/auth/logout").subscribe(
       res => { 
       console.log(res); 
       }, 
       (err: HttpErrorResponse) => { 

       console.log(err.error); 
       console.log(err.name); 
       console.log(err.message); 
       console.log(err.status); 
       } 
      ); 
      } 
     } 

ソリューションファイルです以下のproxy.config.jsonのエントリで解決してください "pathRewrite":{"^/api": ""}最終的なjsonファイルは

0123です
{ 
    "/api/*":{ 

     "target":"http://10.104.40.14:8290/my_app", 
     "secure":false, 
     "pathRewrite": {"^/api" : ""} 
    } 
} 
+0

ブラウザでデベロッパーツールを使用して、リクエストが発行されたかどうかを確認できますか?投稿リクエストにあなたが渡したヘッダが含まれているかどうかを確認できます。 – edkeveked

+0

リクエストURLます。http:// localhost:4200/API /セキュリティ/ログイン リクエスト方法:POST ステータスコード:401権限 リモートアドレス:127.0.0.1:4200 リファラポリシー:ノーリファラー-時にダウングレード レスポンスヘッダー 閲覧元 アクセス制御許可元:* キャッシュコントロール:再検証、キャッシュなし、ストアなし 接続:閉じる コンテンツタイプ:text/html; charset = iso-8859-1 date:Tue、24 Oct 2017 15:01:27 GMT 転送エンコード:チャンク www-authenticate:ベアラ xアプリケーションのコンテキスト:アプリケーション:production:8290 X-Powered-By:Exp RESS –

+0

リクエストヘッダ ソースを表示 受け入れ:アプリケーション/ JSON、text/plainで、*/* 受け入れエンコード:gzipでは、収縮、BR のAccept-言語:EN-US、EN; Q = 0.8 接続:キープ生き のContent-Length:61 のContent-Type:text/plainの X-要求-付:XMLHttpRequestを 要求ペイロード ビューソース {ユーザー名: "[email protected]"、パスワード: "A123456"} パスワード : "a123456" ユーザー名 : "admin @ example。com " 名前 –

答えて

1

このようにあなたのポストの要求を記述してください:

login(username, password, rememberMe)) { 
    const headers = new Headers(); 
    headers.append('Content-Type', 'application/json'); 
    //You can append here whatever you like in your headers; 
    headers.append(username, password); 

    options: RequestOptions = new RequestOptions(headers); 

    this.http.post(/api/security/login, options) 
     .suscribe(res => res.json()) 
    } 

401は、リクエストを処理し、サーバからのエラーです。サーバーがリクエストのヘッダーを取得しているかどうかを確認する必要があります。プロキシの設定について

、次の操作を行うことができます:あなたのURLからapiを削除します

{ "/api": { "target": "yourUrl.com", 
      "secure": false, 
      "pathRewrite": {"^/api" : ""} 
      } 
} 

pathRewriteオプション

+0

httpClientを使用していますので、ユーザー名とパスワードとヘッダーをconstヘッダー= new HttpHeaders({'X-Requested-With': 'XMLHttpRequest'}); content-typeと組み合わせると、複数のヘッダー属性を設定する方法 –

+0

これは角度1の要求ヘッダーequest URLです。 http:// localhost:5002/auth/login リクエスト方法:POST ステータスコード:200 OK リモートアドレス:[:: 1]:5002 リファラーポリシー:no-referrer-when-downgrade –

+0

「RequestOptionsあなたのreqに '{'X-Requested-With': 'XMLHttpRequest'}'を指定することができます〜する。私は私の答えを編集しました。 – edkeveked

関連する問題