How to use token in ionic 2


#1

Here is my error after executing this code

 getSecret() {
 
   var token='06a92668-966c-4ac0-9566-830c34720150';
      let header = new Headers();
      header.append('Authorization', 'Bearer ' + token);
     
 this.http.get(`http://localhost:9090/rest/branches/`,{headers: header}).map(res => res.json())
        .subscribe(
          data => this.branche = data,
          err => err
        );

  }
error

(index):1 XMLHttpRequest cannot load http://localhost:9090/rest/branches/. Response for preflight has invalid HTTP status code 401


#2

It’s a CORS issue. Does it work on a real device with a real URL?


#3

yes with rest client


#4

You already posted the exact same thing in another topic, so I am a bit pissed that you ignore all the responses there and just reposted here…

2 ways to solve this:

  1. Add a service proxy if you are using ionic serve
  2. Or just fix the damn backend to respond with something else than “Status Code 401” to OPTIONS requests to this URL

No other way to fix it.


#5

Ok thanks do not get annoyed
how to add proxy


#6

https://github.com/driftyco/ionic-cli#advanced-serve-options scroll down to “Service Proxies”.


#7

OK thank you I keep you up to date