angular - 无法访问使用 Angular 的 Oauth 2.0 保护的 API
问题描述
我有一个 Angular 项目,它需要访问受 OAuth 2.0 保护的 Web API。
但是,我在访问此 API 时遇到了 CORS 错误。
请在下面找到我的代码:
App.module.ts:
imports
...
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
AuthModule.forRoot({
domain: 'https://test/v2/',
clientId: 'xxx',
clientSecret: 'xxxxx'
}),
],
providers: [{ provide: HTTP_INTERCEPTORS, useClass: AuthHttpInterceptor, multi: true },
AuthService,
HttpClient
],
bootstrap: [
AppComponent]
})
export class AppModule { }
app.component.html
<button class="btn btn-primary btn-block" (click)="getInfo()">
GetInfo
</button>
app.component.ts
let responseData = this.http.get('https://test/v2/xxx');
responseData.subscribe(
response => {
},
error => {
console.log(error)
}
);
我收到以下错误:
从源“http://localhost:4200”访问“https://test/v2/xxx”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“访问控制” -Allow-Origin' 标头出现在请求的资源上。
请帮我。
亲切的问候。
解决方案
推荐阅读
- angular - 角度 4 中的多个 api 调用
- sql - 根据其他列将同一列中的字符串折叠到一个单元格中
- javascript - 如何在本机反应中在单选按钮中提供 fetch api?
- awk - 是否有适用于 linux 的 C/C++ 语法感知搜索/替换命令行工具?
- wordpress - WordPress - add_image_size
- javascript - 没有焦点的新窗口
- javascript - 在对象内映射和显示多个数组 - Reactjs
- java - Firebase 中的 For 循环查询
- ssl - 如何以编程方式为 Tyrus WebSocket @ServerEndpoint 启用 WSS
- r - 将计算/公式添加到 r 中数据框中的列