angular - Angular 中无法访问 Set-Cookie 和自定义标头?
问题描述
我正在开发一个 Web 应用程序,前端使用 Angular 8,后端使用 Java 的 Spring Boot。我的后端使用一个过滤器,它对每个正确的传入 HTTP 调用执行此操作:
public void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain)
throws IOException, ServletException {
// Code of the filter....
res.addHeader("Set-Cookie", "myCookie=myCustomCookie");
res.addHeader("Custom-header", "otherData");
}
这是我的控制器:
@GetMapping("/")
@ResponseStatus(value = HttpStatus.OK)
public String myMethod() {
return "JsonString";
}
现在,我知道如果我设置Set-Cookie
标题,我的调用响应应该在我的浏览器中自动设置 cookie 。如果我使用 Postman 或浏览器,这种情况总是会发生。但是,如果我从 Angular 应用程序进行调用,则不会发生这种情况,并且我在浏览器中找不到myCookie
cookie!
这是角代码:
async doTheCall() {
const myOptions = {
type: ENDPOINT_TYPE.SERVICES,
options: {
observe: 'response',
responseType: 'json',
withCredentials:true
},
pathParams: paramsList
};
return this.get<Configuration>(ENDPOINT_URL.SEARCH_CONFIGURATION, myOptions);
}
Configuration
我的应用程序的对象在哪里。在互联网上我读到通过设置问题解决withCredentials: true
了set-cookie
,但事实并非如此!最后,我无法访问响应中的任何标题(但可以访问正文!)而且我不知道为什么!这样我什至不能使用代码设置cookie,使用Custom-header
标题!这是代码:
let res = await this.doTheCall();
res.subscribe(
response => {
console.log("Headers: ", response.headers);
})
我不知道我错在哪里,因为Postman 向我显示了我回复的所有标题,而且还有很多!
解决方案
推荐阅读
- azure - 从 Azure Key Vault 导出集合证书是否允许您使用安全字符串?它适用于服务器证书
- python - 具有 Keras 数据形状问题的 LSTM 自动编码器
- django - HTML 元素的反射 - django
- c# - 如何序列化和反序列化此 IEnumerable
以便将多个实例存储在一个 XML 文件中? - gcc - gcc 和内联的奇怪行为
- javascript - setState 不更新状态
- node.js - 如何在 NodeJS & ExpressJS 中实现页面控制系统?
- c# - 扩展方法中的表达式树将在本地评估
- c# - 无法从以下位置检索文档:“https://ids.com/.well-known/openid-configuration”
- c - 使用 ioctl 与读取时的 PCIe 总线延迟?