Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular 2 http请求:如何使用用户名、密码和域设置授权_Javascript_Angular - Fatal编程技术网

Javascript Angular 2 http请求:如何使用用户名、密码和域设置授权

Javascript Angular 2 http请求:如何使用用户名、密码和域设置授权,javascript,angular,Javascript,Angular,我有一个从另一台计算机托管的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', '*'

我有一个从另一台计算机托管的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'

在你的服务器上

这是一个服务器端问题。 您必须设置这些标题:

'Access-Control-Allow-Origin'
'Access-Control-Allow-Credentials'
在你的服务器上