2

This question is asked here but still, I could not able to find my answer in those and I believe I have done all my research,

a. Things what I have done so far:

  1. I have tried .share Link for the Same

  2. I have tried to check whether it's a preflight request and found this in my network tab:

enter image description here

there is 2 OPTION call and 2 GET call is going . So, we can be sure that its not PREFLIGHT request.

Now comming towards my coding part:

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());
        }

I am very much sure that this api is not getting called from somewhere else. If somebody stucked in this kind of situation please help me here.

My backend is in Spring Boot and i have implemented CORS filter also:

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() {
    }

}
Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
Rohitesh
  • 1,514
  • 7
  • 28
  • 51
  • 2
    Are you sure the component isn't being rendered twice? In other words being called twice and the constructor is therefore being called twice? – Wesley Coetzee Apr 06 '17 at 12:25
  • @WesleyCoetzee i am sure. i checked it by putting console.log inside the constructor of my component and it's getting printed only once.Can you suggest any way in which i can be sure that component is rendered only once? – Rohitesh Apr 07 '17 at 04:48

1 Answers1

0

Try

constructor( private _notificationService: NotificationService) {
    this.getNotification().subscribe(notifications => {
        this.notificationList = notifications;
    });
 }


getNotification() {
   return this._notificationService.getNotifications();
}

or the workaround of

constructor( private _notificationService: NotificationService) {
    this.getNotification()
     .first()
     .subscribe(notifications => {
         this.notificationList = notifications;
     });
 }


getNotification() {
   return this._notificationService.getNotifications();
}
tubbsy
  • 61
  • 8