Angular 正在将 http 服务 post 代码更新为 Angular 5
Angular Updating http service post code to Angular 5
我正在尝试从我发现的一些发布了一些数据的代码中添加一项服务,但有些代码已经很旧而且我的 angular 版本是 5。
这里是服务代码:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class DataService {
postResponse: any ;
constructor(private http: HttpClient) {}
insertData() {
const headers = new Headers({ 'Content-Type': 'application/json' });
const options = new RequestOptions({ headers: headers });
return this.http.post('http://localhost:1234/dus/mypage.php', JSON.stringify({firstName: 'Joe', lastName: 'Smith333'}),{ headers: headers})
.map((res: Response) => res.json())
.subscribe((res: '') => this.postResponse = res);
}
}
我在使用 headers 和 RequestOptions 时遇到问题。
我该如何解决这个问题?
尝试像这样添加 header--
const headers = new Headers();
headers.append('Content-Type': 'application/json');
如果您在 http post 调用中直接传递 headers,则不需要选项变量。
我正在尝试从我发现的一些发布了一些数据的代码中添加一项服务,但有些代码已经很旧而且我的 angular 版本是 5。
这里是服务代码:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class DataService {
postResponse: any ;
constructor(private http: HttpClient) {}
insertData() {
const headers = new Headers({ 'Content-Type': 'application/json' });
const options = new RequestOptions({ headers: headers });
return this.http.post('http://localhost:1234/dus/mypage.php', JSON.stringify({firstName: 'Joe', lastName: 'Smith333'}),{ headers: headers})
.map((res: Response) => res.json())
.subscribe((res: '') => this.postResponse = res);
}
}
我在使用 headers 和 RequestOptions 时遇到问题。
我该如何解决这个问题?
尝试像这样添加 header--
const headers = new Headers();
headers.append('Content-Type': 'application/json');
如果您在 http post 调用中直接传递 headers,则不需要选项变量。