我从服务器上有两种类型的rest api
1.免费休息api(无认证)
2.具有授权的休息api
对于一种类型的http请求没有任何问题
但是对于具有授权功能的休息api在浏览器中工作但在android设备中不起作用。
我使用以下代码:
let req:RequestOptions=new RequestOptions( {
method: RequestMethod.Get,
url: AppGlobal.OAUTH.USER_AUTHORIZATION_URI,
} );
req.headers = new Headers( {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this.access_token
} );
return this.http.request( new Request( req ) )
.map( res => {
return res.json().message || res.json();
} )
.catch( this.handleError )
答案 0 :(得分:0)
从angular
导入http构造函数中的从'@ angular / http'
导入{Headers,Http,Response}
构造( private jwtService:JwtService,private http:Http){}
设置标题
private setHeaders(): Headers {
let headersConfig = {
'Content-Type': 'application/json',
};
if (this.jwtService.getToken()) {
headersConfig['Authorization'] = `${this.jwtService.getToken()}`;
}
return new Headers(headersConfig);
}
get(route): Observable<any> {
return this.http.get(AppEnvironment.apiUrl + route, {
headers: this.setHeaders()
}).map( res => {
return res.json().message || res.json();
} )
.catch( this.handleError )
post(route): Observable<any> {
return this.http.get(AppEnvironment.apiUrl + route, {
headers: this.setHeaders()
}).map( res => {
return res.json().message || res.json();
} )
.catch( this.handleError )