export class ProfileService {
public apiUrl:string='http://xyz/api';
constructor(private http: HttpClient, private reqHeaders:HttpHeaders) {}
public getProfile(): Observable<any>
{
var reqHeader = new HttpHeaders({
'Content-Type':'application/json',
'Authorization':JSON.parse(localStorage.getItem('token'))
})
return this.http.get(this.apiUrl + '/info.json',{reqHeaders}).map(
(data)=>console.log(data)
)
}
Instead of naming your object reqHeaders
just name it headers
, it should work fine then. Take a look at get
definition:
get<T>(url: string, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<T>;
You can also utilize HttpInterceptors to add authentication to your requests ( more#1 , more#2 ).
By the way, you don't have to set Content-Type to application/json
, because it's the default one.
By the way #2, your title is wrong, because you are asking about Angular, not AngularJS.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.