angular - 项目部署被 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}`,
);
}
}
解决方案
推荐阅读
- mongodb - mongodb - fs.files GridFS 中文件名相同的问题
- c# - 从列表中将对象设置为空
- solr - 如何调试为显示在 solr 管理控制台上的文档和查询字符串计算的分值?
- django - 发布后是否有一种从 django 表单中提取“cleaned_data”的通用方法?这样这可以用于任何 django 形式
- sql - sql 选择查询以获取特定用户的帖子、关注者和关注者的数量
- vue.js - 如何使用 Vuetify Treeview 仅选择父节点(不选择子节点)
- sql-server - SSIS OLEDB 源错误 - 打开 SQL Server 视图的行集失败
- java - 如何在休眠中记录失败的sql?
- machine-learning - 如何识别统计建模中最相关的参数
- node-red - 如何在手机上拍照并使用 Node-RED Dashboard 节点上传