I want to pass multiple parameters to http post method in Angular4. Here each parameter is JSON object. Can any one please explain how to pass the JSON parameters. For my following code Request Payload displaying as data=%5Bobject%20Object%5D&obj1=%5Bobject%20Object%5D.
saveData(data : any): Promise<any> {
let obj1 : any = {'p_id':'2'};
let params = new URLSearchParams();
params.append("data", data);
params.append("obj1", obj1);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post('urlContent', params, options).toPromise()
.then(response => response.json())
.catch(this.handleError);
}
答案 0 :(得分:1)
我没有使用URLSearchParams(),而是使用了数组并传递了数据。它对我有用。
saveRtlList(data : any): Promise<any> {
let obj1 : any = {'p_id':'2'};
let savedata : Array<any> = [];
savedata.push(JSON.stringify(data));
savedata.push(obj1 );
let values : any = {} as any;
values.param = savedata;
return this.http.post(url, {}, {params:vals}).toPromise()
.then(response => response.json())
.catch(this.handleError);
}
答案 1 :(得分:0)
try this :
saveData(data : any): Promise<any> {
let obj1 : any = {'p_id':'2'};
let body= {obj1 ,data};
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post('urlContent', body, options).toPromise()
.then(response => response.json())
.catch(this.handleError);
}