如何在IONIC上提交表格进行数据POST?

时间:2017-08-17 07:00:48

标签: json angular typescript ionic-framework

我正在尝试对离子提交一个带有数据数组的表格,但我不知道我怎么能这样做(但如果我检查在POSTMAN中这样做实际上有效)。

PETITION POST

我尝试使用此表单,但无效:

  submitRegistration(value):void{

var headers = new Headers();
let options = new RequestOptions({headers: headers});
headers.append("Content-Type", 'application/json');

let link = 'http://apidata.com/';

let myData = {
  fos_user_registration_form: [{
    _token: this.data.token,
    username: value.usuario,
    email: value.correo,
    plainPassword: [{
      first: value.password,
      second: value.confirmPassword
    }],
    userLocalization: value.municipio}
  ]};

console.log(myData);

this.http.post(link, myData, options)
  .subscribe(data => {
    this.data.response = data["_body"];
  }, error => {
    alert("Oooops!");
  });

}

任何人都可以帮助我吗? Thabks

2 个答案:

答案 0 :(得分:0)

你必须在发布之前对数据进行字符串化,试试这个

this.http.post(link, JSON.stringify(myData), options)
  .subscribe(data => {
    this.data.response = data["_body"];
  }, error => {
    alert("Oooops!");
  });

它应该做的伎俩

答案 1 :(得分:0)

最后我找到了解决方案(对我而言)。这里是代码

  submitRegistration(value):void{

var headers = new Headers();
let options = new RequestOptions({headers: headers});
headers.append("Content-Type", 'application/x-www-form-urlencoded');

let link = 'http://APIREST.com/';

let myData = 'fos_user_registration_form[_token]='+encodeURI(this.data.token);
myData += '&fos_user_registration_form[username]='+encodeURI(value.usuario);
myData += '&fos_user_registration_form[email]='+encodeURI(value.correo);
myData += '&fos_user_registration_form[plainPassword][first]='+encodeURI(value.password);
myData += '&fos_user_registration_form[plainPassword][second]='+encodeURI(value.confirmPassword);
myData += '&fos_user_registration_form[userLocalization]='+encodeURI(value.municipio);

console.log(myData);

this.http.post(link, myData, options)
  .subscribe(data => {
    this.data.response = data["_body"];
    console.log(data);
  }, error => {
    console.log("Oooops!");
  });

}