我写了一个代码来从第3方API中获取数据。它已在邮递员收藏和Firefox浏览器中使用。但是在谷歌浏览器中,即使启用了CORS扩展程序,它也会显示CORB错误。
ts文件,名为popup.component.ts
import { Component, OnInit } from '@angular/core';
import { PopupService } from './popup.service';
@Component({
selector: 'app-popup',
templateUrl: './popup.component.html',
styleUrls: ['./popup.component.scss']
})
export class PopupComponent implements OnInit {
rowData: any = [];
constructor(
private popupService: PopupService
) { }
ngOnInit() {
this.GpGetAllValues();
}
GpGetAllValues() {
this.popupService.GpGetAllValues()
.subscribe(
data => {
console.log('successfully get all data --- ', data);
this.rowData = data.categories;
console.log('after set the categories are ----- ', this.rowData);
},
error => {
console.log('cannot able to get all data --- ', error);
}
);
}
}
服务文件popup.service.ts
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class PopupService {
constructor(
private http: HttpClient
) { }
GpGetAllValues(): Observable<any> {
return this.http.get(`https://api.stlouisfed.org/fred/category?category_id=125&api_key=1d6109900692021b3c0e18d9a1c9591f&file_type=json`);
}
}
在上面的屏幕截图中。我启用了CORS扩展程序,但出现错误。
答案 0 :(得分:0)
尝试关闭浏览器并杀死所有chrome实例,然后从cmd提示符[chrome.exe“ --disable-web-security]重启浏览器