Angular 角度2休息服务请求

Angular 角度2休息服务请求,angular,rest,angular2-services,Angular,Rest,Angular2 Services,我有一个运行良好的.net Wep API服务。当我想从Angularjs应用程序获取数据时,浏览器控制台给了我这个错误 getCurrentFeed() :Observable<Tweet[]>{ return this.http.get('http://localhost:6010/GetTweets').map((resp :Response)=>{ console.log(resp.json()); var fetchedTweets=[];

我有一个运行良好的.net Wep API服务。当我想从Angularjs应用程序获取数据时,浏览器控制台给了我这个错误

getCurrentFeed() :Observable<Tweet[]>{

  return this.http.get('http://localhost:6010/GetTweets').map((resp :Response)=>{
    console.log(resp.json());
    var fetchedTweets=[];
    for(let tweet of resp.json().data)
    {
     fetchedTweets.push(this.getTweetFromJson(tweet));

    }
    return fetchedTweets as Array<Tweet>;
});

}
getCurrentFeed():可观察{
返回此.http.get('http://localhost:6010/GetTweets“).map((响应)=>{
log(resp.json());
var fetchedTweets=[];
for(让我们推特resp.json().data)
{
fetchedTweets.push(this.getTweetFromJson(tweet));
}
以数组的形式返回fetchedTweets;
});
}
无法加载XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。
error_handler.js:54异常:URL:null的状态为0的响应

由于您的客户端应用程序由运行在端口4200上的开发服务器提供服务,因此不允许客户端访问其他服务器,除非它们被专门配置为允许访问您的客户端

在开发模式下,您不必配置服务器的头。只需在客户端上配置一个简单的代理。在项目根目录中创建proxy-conf.json文件,包含以下内容:

{
  "/GetTweets": {
    "target": "http://localhost:6010",
    "secure": false
  }
} 
然后从您的代码中删除要删除的DNS全名

this.http.get('/GetTweets') 
最后,按如下方式运行应用程序:

ng serve --proxy-config proxy-conf.json

运行在4200上的服务器将把URL中包含/GetTweets的get请求重定向到运行在6010上的服务器,您不会收到此错误。

我如何在此处应用标头非常感谢您的回答,对我来说效果很好