簡體   English   中英

Angular 2 http請求:如何使用用戶名,密碼和域設置授權

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM