- 在API网关上配置跨域策略,允许来自 Angular 应用程序的请求。例如,以下是一个允许所有来源访问的示例:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowAllOrigins",
"Effect": "Allow",
"Principal": "*",
"Action": "execute-api:Invoke",
"Resource": "arn:aws:execute-api:region:account-id:api-id/*",
"Condition": {
"IpAddress": {
"aws:SourceIp": [
"0.0.0.0/0"
]
}
}
}
]
}
- 在 Angular 应用程序中使用代理配置来跨域。例如,以下是 proxy.conf.json 文件的示例,用于将所有以 /api 开头的请求代理到 API 网关:
{
"/api/*": {
"target": "https://api-gateway-endpoint.com",
"secure": true,
"changeOrigin": true
}
}
- 在发送 POST 请求时,将要发送的数据转换为 FormData,然后将其作为请求的 body 发送,例如:
postData() {
const data = new FormData();
data.append('username', 'johndoe');
data.append('password', 'secret');
this.http.post('https://api-gateway-endpoint.com/api/login', data).subscribe(response => {
console.log(response);
});
}