2016-12-06 7 views
1

私は別のマシンからホストされたAPIメソッドを持っています。ブラウザ上で使用する際には、以下のようにユーザ名とパスワードを入力して接続できます:Angularjs 2 httpリクエスト:設定方法ユーザ名、パスワード、ドメインでの認証

enter image description here

しかし、私はhttp Angularjs 2コンポーネント上でそのメソッドを呼び出す方法を知りません。ここで

は私のコードです:ここでは

this.headers = new Headers(); 
this.headers.append('Content-Type', 'application/json; charset=utf-8'); 
    this.headers.append('Access-Control-Allow-Origin', '*'); 
    this.headers.append('Access-Control-Allow-Credentials', true); 
    this.headers.append('Authorization', 'Basic ' + btoa('myUsername:myPassword')); 
    var requestOptions = new RequestOptions({ headers: this.headers, withCredentials: true }); 
    return this.http.get(urlParam, requestOptions) 
     .toPromise() 
     .then(response => JSON.parse(response.json())) 
     .catch(this.handleError); 

は、私が得たエラーです:

XMLHttpRequest cannot load http://192.168.2.103:44411/api/Profile/user/1. Response to preflight request doesn't pass access control check: A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:6823' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute. 

私を助けてください。ありがとう

答えて

0

これはサーバー側の問題です。サーバー上

'Access-Control-Allow-Origin' 
'Access-Control-Allow-Credentials' 

: あなたはこれらのヘッダを設定する必要があります!

関連する問題