小编Jan*_*ara的帖子

调用第 3 方 API 时处理 CORS

是的,这是一个非常有名的问题,我尝试了之前堆栈溢出 QnA 中提到的许多方法,但没有任何效果。我正在尝试在我的应用程序中使用BANZAI-Cloud API,但它给出了以下错误 在此处输入图片说明

这是我的服务类代码

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import { Observable } from 'rxjs';
import { HttpHeaders } from '@angular/common/http';

@Injectable({providedIn:"root"})
export class PriceTableService{
   
    private priceurl = "https://banzaicloud.com/cloudinfo/api/v1/providers/google/services/compute/regions/asia-east2/products"
    // private priceurl = "https://jsonplaceholder.typicode.com/posts"
    
    constructor(private http:HttpClient){}
    httpOptions = {
        headers: new HttpHeaders({
            'Access-Control-Allow-Methods':'DELETE, POST, GET, OPTIONS',
            'Access-Control-Allow-Headers':'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With',
            'Content-Type':  'application/json',
            'Access-Control-Allow-Origin':'http://localhost:4200'
        })
      };
    getPrices(){
            this.http.get(this.priceurl,this.httpOptions).subscribe(result=>{
            console.log(result);
            return(result);
        })
     }
    ngOnInit() {
    }
}
Run Code Online (Sandbox Code Playgroud)

API可在Angular 应用程序中使用POSTMANCHROME但无法将数据获取到我的 …

cors angular-httpclient

3
推荐指数
2
解决办法
5120
查看次数

标签 统计

angular-httpclient ×1

cors ×1