【发布时间】:2021-12-26 21:52:23
【问题描述】:
我正在为我的大学制作一个带有 java 后端的 angular 12 应用程序。 我正在测试 Angular 的 http 客户端,但我无法发出任何请求,因为它被 CORS 阻止了。 首先,我尝试向我的后端服务器发出 POST 请求,但没有成功。 角度代码:
const API_URL = 'http://localhost:9080'
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(private http:HttpClient, private router:Router) { }
login(user:User) {
this.http.post(`${API_URL}/login`, user, {observe:'response'}).subscribe(result => {
console.log(result)
})
}
所以,我搜索了一下,发现这个大部分是服务器端的,虽然我可以在 Insomnia/Postman 中正常发出请求,但我还是尝试在我的 server.xml 上配置 cors。
但后来我有了尝试使用外部 API 的想法。 我尝试向 Google 发出 GET 请求,结果却得到了回复:
每个外部 API 都会发生这种情况,所以我不认为问题出在服务器端,而是 Angular,但我找不到任何不会说问题出在服务器端的解决方案,但不可能,因为我不能向任何网站提出任何请求,对吧?
【问题讨论】:
标签: angular api cors integration