2016-12-06 78 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' 

在您的服務器上!

相關問題