我对这些代码感到麻烦,我创建了一个包含以下代码块的标题
headers.append("Authorization",btoa(username+":"+password));
var requestOptions = new RequestOptions({headers:headers});
但是如果我尝试在那个
的post方法中使用它return this.http.post(url,JSON.stringify({username,password}),requestOptions)
.map(res=>res.json())
.map(res=>{
if(res){
localStorage.setItem("isLogged",res);
this.loggedIn =true;
}
return res;
});
我收到此错误消息
Typescript Error
Argument of type 'RequestOptions' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'headers' are incompatible. Type 'Headers' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'. Type 'Headers' is not assignable to type '{ [header: string]: string | string[]; }'. Index signature is missing in type 'Headers'.
我尝试将Header()更改为HttpHeader(),但它没有帮助。有什么问题?
已更新
我删除了requestOptions对象,然后我从HttpHeaders()
创建了标头let headers = new HttpHeaders();
并在
中的post方法中使用此标头值return this.http.post(url,JSON.stringify({username,password}), { options: { headers: headers; } })
.map(res=>res.json())
.map(res=>{
if(res){
localStorage.setItem("isLogged",res);
this.loggedIn =true;
}
return res;
});
然后得到此错误
[ts]
Argument of type '{ options: { headers: HttpHeaders; }; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
Object literal may only specify known properties, and 'options' does not exist in type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
我也试过这个
return this.http.post(url,JSON.stringify({username,password}), { headers: headers })
.map(res=>res.json())
.map(res=>{
if(res){
localStorage.setItem("isLogged",res);
this.loggedIn =true;
}
return res;
});
然后我在第一个“.map”
上收到错误[ts] Property 'map' does not exist on type 'Observable<Object>'.
答案 0 :(得分:4)
RequestOptions
类将与已弃用的 Http 模块一起使用,因为您收到此错误,我认为您正在使用 HttpClient 模块。
如果您要将代码中显示的headers
设置为options
,则可以使用类似的内容(Angular docs中显示的简化版本):
request(url, { body }, { options: { headers?: HttpHeaders; } })
但是,您也可以直接设置headers
,而无需使用选项。这看起来像这样:
request(url, { body }, { headers?: HttpHeaders; } )
答案 1 :(得分:3)
@ angular / http已被弃用
使用@ angular / common / http代替
import { HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'my-auth-token'
})
};
addHero (hero: Hero): Observable<Hero> {
return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
.pipe(
catchError(this.handleError('addHero', hero))
);
}
答案 2 :(得分:0)
import {
Http,
Response,
Headers,
RequestOptions
} from '@angular/http';
private headers = new Headers({
'Content-Type': 'application/json',
'Authorization': localStorage.getItem('token')
});
private options = new RequestOptions({
headers: this.headers
});
private user: User;
constructor(private _http: Http) {}
getUserService() {
return this._http.get(this.baseUrl + '/user', this.options)
.map((response: Response) => response.json(),
error => error.json('erreur dans lurl'));
}
答案 3 :(得分:0)
服务中
以简单的方式传递多个参数
export interface Doctor {
license1d1: number;
firstname1: string;
experience1: number;
fee1: number;
qualification1: string;
lastname1: string;
specialization1: string; }
getDoctorsBydepartment(Hosp: number, Dept: number): Observable<Doctor[]> {
const url = 'http://192.168.75.15:8080/Booking/rest/List.do?'
ClinicID='+Hosp+'&DeptID='+Dept;
return this.http.get<Doctor[]>(url).pipe()
}