请求本地资源时发生CORS错误

时间:2019-05-27 12:55:02

标签: angular typescript cors

我正在尝试在本地计算机上进行延迟测试,并不断收到不允许的CORS错误。我已经尽力在这里找到所有内容,所以请不要再将我引向另一篇文章。由于我要在我们的工作域中对本地计算机执行ping操作,因此无法使用代理访问我们的工作网络。有没有办法解决?如何添加所需的标题?

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import {Http} from '@angular/http';
import { Observable } from 'rxjs';
import { interval } from 'rxjs';
import { HttpClient, HttpHeaders, HttpClientModule } from ' 
@angular/common/http';



@Injectable()
export class PingService {
pingStream: Subject<number> = new Subject<number>();
ping: number = 0;
headers = new HttpHeaders().set('access-control-allow-origin', 'cstorch- 
3420');


constructor(private _http: Http) {
interval(5000)
  .subscribe((data) => {
    let timeStart: number = performance.now();

    this._http.get(this.url)
      .subscribe((data) => {
        let timeEnd: number = performance.now();

        let ping: number = timeEnd - timeStart;
        this.ping = ping;
        this.pingStream.next(ping);
      });
  });
  }
}

1 个答案:

答案 0 :(得分:0)

您需要在后端允许CORS。

例如(ASP.NET后端):

 config.EnableCors(new EnableCorsAttribute("*", "*", "*"));