我试图从Angular中获取API端点以获取JSON数据。我的代码如下:
import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import { HttpHeaders} from '@angular/common/http';
@Component({
selector: 'app-api-caller',
templateUrl: './api-caller.component.html',
styleUrls: ['./api-caller.component.css']
})
export class ApiCallerComponent implements OnInit {
// works with 'https://api.github.com/users/seeschweiler'
ngOnInit(): void {
const options = { headers: new HttpHeaders().set('Content-Type', 'application/json') };
this.http.get('https://api.upsie.com/api/helloworld', options).subscribe(
data => {
console.log(data);
},
err => {
console.log('An Error occured');
});
}
constructor(private http: HttpClient) {
}
}
浏览器控制台显示错误:
"无法加载资源:预检响应未成功"是什么意思,我该如何解决?