这个问题在这里被问到,但是,我无法在那些问题中找到答案,我相信我已完成了所有的研究,
一个。我到目前为止所做的事情:
我尝试过.share Link for the Same
我试图检查它是否是预检请求,并在我的网络标签中找到了这个:
有2个OPTION调用和2个GET调用。所以,我们可以肯定 它不是PREFLIGHT请求。
现在转向我的编码部分:
Component
------------
constructor(
private _notificationService: NotificationService) {
this.getNotification();
}
getNotification() {
this._notificationService.getNotifications().subscribe(notifications => {
this.notificationList = notifications;
});
}
Service:
-------------
constructor(private http: Http,private config: Configuration) { }
getNotifications() {
const headers = new Headers({
'c-t-tenant': 'TENANT NAME',
'c-t-apitoken': 'TOKEN-NAME',
'Content-Type': 'application/json',
});
return this.http.get(this.config.notificationApiUrl, { headers: headers })
.map((res: Response) => res.json());
}
我非常确定这个API不会从其他地方调用。 如果有人遇到这种情况,请在这里帮助我。
我的后端是Spring Boot,我也实现了CORS过滤器:
package com.bosch.cb.dashboard.auth;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
@Component
public class SimpleCORSFilter implements Filter {
public SimpleCORSFilter() {
System.out.println("SimpleCORSFilter init");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Pragma, Origin,Accept, b-h-apitoken ,X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
答案 0 :(得分:0)
尝试
constructor( private _notificationService: NotificationService) {
this.getNotification().subscribe(notifications => {
this.notificationList = notifications;
});
}
getNotification() {
return this._notificationService.getNotifications();
}
或
的解决方法constructor( private _notificationService: NotificationService) {
this.getNotification()
.first()
.subscribe(notifications => {
this.notificationList = notifications;
});
}
getNotification() {
return this._notificationService.getNotifications();
}