angular - Angular 无法读取后端发送的 cookie
问题描述
我想使用 JAX-WS 和 Jersey 从我的后端发送一个简单的 cookie 到我的 Angular 网络客户端,但 Chrome 无法读取或设置 cookie。
@GET
@Path("/cookie")
@Produces(MediaType.TEXT_PLAIN)
public Response getId(@CookieParam("session") String inp)
{
if (!keys.contains((inp))) {
String cipher = randomize();
keys.add(cipher);
return Response.ok().cookie(new NewCookie("session", cipher)).build();
}
return Response.ok().build();
}
这就是我在后端的做法
private httpHeaders = new HttpHeaders()
.set('Accept', 'text/plain')
.set('Content-Type', 'text/plain');
private options = {
headers: this.httpHeaders
};
constructor(private httpClient: HttpClient) { }
getId(){
return this.httpClient.get(this.apiUrl+'session/cookie', this.options);
}
这就是我在客户端管理它的方式。
解决方案
First you can double check wether if the API is providing data accordingly to the request with POSTMAN call. If So you can come to conclusion that there is nothing wrong in the API.
Secondly I see inside your getId()
method while you are using the httpClient
you doest actually subscribe the to the get
request.
You need to subscribe
to the request in order to fetch values from the API and inititate the request.
getId(){
return this.httpClient.get(this.apiUrl+'session/cookie',
this.options).subscribe((token)=>{
console.log(token)
});
}
推荐阅读
- r - 在网页抓取中找不到网页,我如何在 R 中找到它?
- python - Python -- 矩阵中的行[2],加密函数
- animation - 使用 Sass 旋转元素圈
- sql - 在 HeidiSQL 中添加外键时遇到问题(错误 1215)
- c#-4.0 - 我需要在单个传出声明中使用 c# 从 ADFS 获取所有用户详细信息
- angular - 以角度处理复选框/类别组
- python - 运行 train.py 文件时训练期间发生 StopIteration 错误
- spartacus-storefront - Spartacus 3.2 - 产品图片在购物车页面中的数量变化时闪烁
- wpf - WPF Visual Studio:主窗口无法缩放以适应 Windows 屏幕
- python - 将数据从 CSV 文件导入字典,其中键是国家名称,值是 np.arrays