问题
I want to Pass parameters to POST method in angular 2 but it is not working whwn I used params:new httpParams().set() but it is not worked .I also tried params:new httpParams().set() and headers then also it is not worked & finally I got the solution with Formdata
回答1:
You can use formData to send http params or any kind of attachment.
let formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');
// http request with post method
this.httpService.post('Url', formData);
回答2:
Their is way to pass data to post method in angular 2 by using the Formdata,while passing the data their is no need of headers because we are using the formdata here. To understand this concept lets take one example as-
saveNewData(name, countryId) {
this.saveNewDataUrl = 'localhost:7575/app/data';
const fd = new FormData();
fd.append('name', name);
fd.append('countryId', countryId);
return this.http.post(this.saveNewDataUrl, fd);
}
回答3:
You can do it like below:
var validFileExtensions: string[] = ['jpg', 'jpeg','png'];
this.uploadedFiles.append('id', this.userInfo.id.toString());
this.uploadedFiles.append('validImageExtensions', validFileExtensions.toString());
this.accountService.uploadImage(this.uploadedFiles).subscribe((result: any) => {
this.alertService.success("Profile settings updated successfully");
},
来源:https://stackoverflow.com/questions/50409279/how-to-pass-params-to-post-method-in-angular-2