【问题标题】:How to pass params to POST method in angular 2如何在角度 2 中将参数传递给 POST 方法
【发布时间】:2018-05-18 10:28:37
【问题描述】:

我想在角度 2 中将参数传递给 POST 方法,但当我使用 params:new httpParams().set() 时它不起作用,但它不起作用。我也试过 params:new httpParams().set() 和 headers 然后它也不起作用,最后我得到了 Formdata

的解决方案

【问题讨论】:

  • link你检查了吗?
  • 是的,我检查了该链接,但它无法正常工作
  • 以下是我的问题的正确答案

标签: javascript angular post parameters angular2-services


【解决方案1】:

您可以使用 formData 发送 http 参数或任何类型的附件。

let formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');

// http request with post method
this.httpService.post('Url', formData);

【讨论】:

    【解决方案2】:

    他们是通过使用 Formdata 将数据传递给 Angular 2 中的 post 方法的方法,而传递数据则不需要标题,因为我们在这里使用的是 formdata。 为了理解这个概念,让我们举一个例子-

        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】:

      你可以这样做:

      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");
            },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-04
        • 2016-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-17
        相关资源
        最近更新 更多