How to pass params to POST method in angular 2

女生的网名这么多〃 提交于 2019-12-23 16:17:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!