如何解决项目部署被 CORS 政策阻止
我想使用 Ionic 部署 PWD,但我无法与 API 进行通信以返回请求的信息。
我认为问题在于通过 HTTP 请求访问的安全性。
我的服务代码:
import { Injectable } from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import { ResponseTopHeadlines } from '../models/models';
import { environment } from 'src/environments/environment';
const apiAkey = environment.apiKey;
const apiUrl = environment.apiUrl;
const headers = new HttpHeaders({
'X-Api-Key': apiAkey,});
@Injectable({
providedIn: 'root',})
export class NewsService {
headLinesPage = 0;
categoryAct = '';
categoryPg = 0;
constructor(private http: HttpClient) {}
private startQuery<T>(query: string) {
query = apiUrl + query;
return this.http.get<T>(query,{ headers });
}
getTopHeadLines() {
this.headLinesPage++;
return this.startQuery<ResponseTopHeadlines>(
`/top-headlines?country=us&page=${this.headLinesPage}`,);
}
getTopHeadLinesCategory(category: string) {
if (this.categoryAct === category) {
this.categoryPg++;
} else {
this.categoryPg = 1;
this.categoryAct = category;
}
return this.startQuery<ResponseTopHeadlines>(
`/top-headlines?country=us&category=${category}&page=${this.categoryPg}`,);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。