如何在ionic3中的POST API中传递数据?

时间:2018-03-09 06:55:08

标签: ionic3

我已经通过JSON格式传递了API中的数据,但每次都显示错误来自响应,但是在Postman API中获得成功,我不明白我错在哪里

//This is JSON Obejt and pass in the the API 
let object = {
  "firmName"          : this.createAccountForm.value.firmName,
  "personName"        : this.createAccountForm.value.yourName,
  "email"             : this.createAccountForm.value.email,
  "phoneNumber"       : Number(this.createAccountForm.value.mobileNumber),
  "gstNo"             : Number(this.createAccountForm.value.gstNumber),
  "userType"          : this.createAccountForm.value.selectedUserType,
  "isByInvitation"    : false
}

  let headers =  {
    headers: new  HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded'})
  };

  //Pass the data into the API and create the user
  if(url != "" && url != undefined){
    this.http.post(url, object, headers).subscribe(data => {
      console.log(data);
    }, error => {
      console.log(error);
    });
	}

1 个答案:

答案 0 :(得分:0)

请替换此代码我希望您获得完整的结果

      //This is JSON Obejt and pass in the the API 
let object = {
  "firmName="          + this.createAccountForm.value.firmName +
  "&personName="        + this.createAccountForm.value.yourName +
  "&email="             + this.createAccountForm.value.email +
  "&phoneNumber="       + Number(this.createAccountForm.value.mobileNumber)+
  "&gstNo="             + Number(this.createAccountForm.value.gstNumber) +
  "&userType="          + this.createAccountForm.value.selectedUserType +
  "&isByInvitation="    + false
}

  let headers =  {
    headers: new  HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded'})
  };

  //Pass the data into the API and create the user
  if(url != "" && url != undefined){
    this.http.post(url, object, headers).subscribe(data => {
      console.log(data);
    }, error => {
      console.log(error);
    });
    }