0

I want to post the data from an angular formly form to API and I dont know much about fomly and ionic2 combination.The code I tried is not sending my form data to API it is printing error on console.

My .ts code is:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {Validators, FormGroup} from '@angular/forms';
import {FormlyFieldConfig} from 'ng-formly';
import { Http,Headers,RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';


@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {

post = [];

constructor(public navCtrl: NavController,private http : Http) {

}

form: FormGroup = new FormGroup({});
  userFields: FormlyFieldConfig = [{
    className: 'row',
    fieldGroup: [{
        className: 'col-xs-6',
        key: 'identity',
        type: 'input',
        templateOptions: {
            type: 'email',
            label: 'Email address',
            placeholder: 'Enter email'
        },
        validators: {
          validation: Validators.compose([Validators.required])
        }
    }, {
        className: 'col-xs-6',
        key: 'password',
        type: 'input',
        templateOptions: {
            type: 'password',
            label: 'Password',
            placeholder: 'Password',
            pattern: ''
        },
        validators: {
          validation: Validators.compose([Validators.required])
        }
    }]
  }];

  user = {

  };

  submit(user) {

      let url = "http://urbanholic.com/drago/index.php/api/users/login";





          let data =  {"identity":" ","password":" "}
              let headers = new Headers({
                  'Content-Type': 'application/x-www-form-urlencoded'
              });
               let options = new RequestOptions({ headers: headers, method: "post" });


              this.http.post(url,data,options)
                  .map(res =>  res.json())
                  .subscribe(
                      data => {
                              this.post= data.CarRental;
                              console.log(this.post);


                              },
                     /** data => {console.log(data);},*/
                      err => console.log("error"),

                  );
    }

  }

html code is:

 <form  [formGroup]="form" (ngSubmit)="submit(user)">
    <formly-form [model]="user" [fields]="userFields">
      <button type="submit" class="btn btn-default" >Button</button>
    </formly-form>
  </form>
3
  • Hi there, please provide the error you got (complete traceback)
    – Arount
    Jul 21, 2017 at 8:53
  • err => console.log("error"), this line is executing
    – swarnima
    Jul 21, 2017 at 9:18
  • Can you change that line to err => console.log(err) to see an actual error?
    – Jamie Rees
    Jul 21, 2017 at 10:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.