[英]How to send authorization header to a http request in angular js (version 1.4.8)
[英]Angular 2 http request: how to set Authorization with username, password and domain
我有一個從另一台機器托管的API方法,我可以在瀏覽器上使用它時連接到它,輸入用戶名和密碼如下:
但我不知道如何使用Http在Angular 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.
請幫我。 謝謝
這是服務器端問題。 你必須設置這些標題:
'Access-Control-Allow-Origin'
'Access-Control-Allow-Credentials'
在你的服務器上!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.