使用Angular中的observable和Promise创建一个API,如:
submitEnquiry(details){
console.log(JSON.stringify(details));
return new Promise((resolve, reject) => {
let headers = new Headers();
console.log("TOKEN"+this.token);
headers.append('Content-Type', 'application/json');
this.http.post(this.apiUrl+"api/enquiry/request?token="+this.token, JSON.stringify(details), {headers: headers})
.subscribe(res => {
let data = res.json();
resolve(data);
}, (err) => {
reject(err);
});
});
}
第二种方法:
submitEnquiry(details):Observable<any> {
const headers: HttpHeaders = new HttpHeaders();
console.log(details);
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer '+this.authService.token);
//return this.httpClient.get<Product[]>('http://13.126.17.194/colleges.php', {headers: headers.append('Authorization', this.authService.token)});
return this.httpClient.post<any>(this.apiUrl+"api/enquiry/request?token="+this.authService.token, JSON.stringify(details),{headers:headers});
}
我面临的问题是。第一个工作和JSON数据输入到服务器,而当我使用后一个时,它给我无效的请求错误!我在这里错过了什么?我知道最好的方法是使用observable ..