I've been trying for days and I can not get the following method in ionic 2 .
getMy() {
return this.http.get("users/my", this.setAuthorization(this.headers))
.toPromise()
.then(res => res.json())
.catch(this.handleError);
}
protected setAuthorization(headers) {
headers.append("Authorization", "Bearer " + localStorage.getItem("token"));
return {headers: headers};
}
In my request authorization is simply not being sent:
Edit1DamonDudekdecidedtodootherwiseasIamwiththeionic2versionthatusetheangle4.1.3,iedoesnothavetheinterceptorsIhaveyetimplementedmyownfornowandthisisso
import{Injectable}from'@angular/core';import{Http,ConnectionBackend,Headers,RequestOptions,Request,RequestOptionsArgs}from'@angular/http';@Injectable()exportclassBaseProviderextendsHttp{privateheaders:Headers=newHeaders();privateurlApi:string="*********";
constructor(backend?: ConnectionBackend, defaultOptions?: RequestOptions)
{
super(backend, defaultOptions);
this.headers.append("Content-Type", "application/x-www-form-urlencoded;
charset=UTF-8");
}
request(request: Request, options?: RequestOptionsArgs) {
if(localStorage.hasOwnProperty("token")) {
this.headers.append("Authorization", "Bearer " +
localStorage.getItem("token"));
}
request.url = this.urlApi + request.url;
request.headers = this.headers;
return super.request(request, options);
}
}
Notice how my request was in the chrome log
XMLHttpRequest cannot load *********. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
I just noticed something when I do the request in postman as OPTIONS does not return anything to me anymore as GET returns something to me, I think the problem is that angular is forcing my request to be OPTIONS. Is anyone able to help me?