我已将应用程序从 Angular 4.2 升级到 5,但在更改Http
为HttpClient
POST 请求时出错:
错误服务器响应状态为 415(不支持的媒体类型)
在app.module
我已经导入HttpClientModule
:
import { HttpClientModule } from '@angular/common/http';
旧代码:
post(url: string, model: any): Observable<any> {
let body = JSON.stringify(model);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this._http.post(url, body, options)
.map((response: Response) => <any>response.json())
.catch(this.handleError);
}
新代码:
put(url: string, id: number, model: any): Observable<any> {
let body = JSON.stringify(model);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options: any = new RequestOptions({ headers: headers });
return this._http.put(url + id, body, options)
.catch(this.handleError); //only removed .map
}
谢谢