以角度4发布请求问题

时间:2018-04-12 09:20:20

标签: angular

我遇到此问题“对预检请求的响应未通过访问控制检查:请求的资源上没有'Access-Control-Allow-Origin'标头”这是我的代码

 constructor(public http:Http,private baseUrls:BaseUrls) { 

    this.headers.append('Access-Control-Allow-Origin' , '*');
    this.headers.append('Access-Control-Allow-Headers', 'Content-Type');
    this.headers.append('Accept','application/json');
    this.headers.append('content-type','application/json');
    this.options = new RequestOptions({ headers: this.headers });

  }




     getFeaturedDeals()
    {

       return 
       this.http.post(
       'http://localhost:8560/AppApi/Home/FeaturedDeals',
       {
    Index: 0, 
    Count: 4},
    this.options).
       map((res)=>{

    return res.json();
 }) }

2 个答案:

答案 0 :(得分:0)

服务器负责允许或禁止客户端发出的请求(在您的情况下为Angular)。因此,需要由服务器设置Access-Control-Allow-Origin(和相关)标头。

答案 1 :(得分:0)

跨源资源共享(CORS)是一种安全机制,默认情况下会阻止对另一个域的请求。您应该设置响应标头以允许Angular域访问您的后端应用程序。

您使用的是哪种后端语言?

For .NET you can use this article.