import { Http } from ‘@angular/http’;
import { Headers, RequestOptions, URLSearchParams } from ‘@angular/http’;
首先要import 这几个模块,
分别是http请求体,http请求头部,请求options,和请求发送的data,
import { Http } from '@angular/http';
import { Headers, RequestOptions, URLSearchParams } from '@angular/http';
import 'rxjs/add/operator/toPromise';
constructor(private http: Http) {
this.headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' });
this.options = new RequestOptions({ headers: this.headers });
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
public getImg(): Promise<object> {
const data = new URLSearchParams();
const bodyData = {
'UUID': 'nullfy6HS'
};
data.append('body', JSON.stringify(bodyData));
data.append('tradeCode', '0002');
data.append('tradeType', 'authService');
return this.http.post(this.baseUrl, data, this.options)
.toPromise().then(response => response.json()).catch(this.handleError);
}
返回一个promise函数体