要在Angular HttpClient中设置跨域cookie,需要在请求中设置{ withCredentials: true }。以下是代码示例:
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) { }
this.http.get('https://api.example.com/data', { withCredentials: true }) .subscribe((response) => { console.log(response); });
在这个示例中,我们使用了带有一个布尔值的第二个参数。如果设置为true,则表示可以发送cookies和认证头信息。这将允许浏览器从远程站点接收和存储cookie。
请注意,此选项需要远程服务器的支持。如果远程服务器不允许发送cookies,这个选项将不起作用。
使用withCredentials选项时,浏览器将在请求的请求头中添加一个标志,并将cookie添加到响应的响应header中。