form3.ts 3.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import {Component} from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Student } from '../students/student';

import {
    FormControl,
    FormGroup
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'form3',
    template: `
  <div>
    <form #studentform="ngForm" (submit)="onSubmit(studentform)" class="col-sm-8">
      <div class="form-group">
        <label>Όνομα</label>
        <input type="text" [(ngModel)]="student.name" name="name"
          required class="form-control" />
      </div>
      <div class="form-group">
        <label>Επώνυμο</label>
24
        <input type="text" [(ngModel)]="student.studentsurname" name="studentsurname"
25 26
          required class="form-control" />
      </div>
27
      <!--
28 29 30 31 32
      <div class="form-group">
        <label>Διεύθυνση</label>
        <input type="text" [(ngModel)]="student.address" name="address"
          required class="form-control" />
      </div>
33

34 35 36 37 38
      <div class="form-group">
      <label>Ημερομηνία Γέννησης</label>
      <input type="date" [(ngModel)]="student.birthdate"
       class="form-control" name="birthdate" />
      </div>
39 40
      -->

41 42 43 44 45 46 47 48 49 50
      <input type="submit"  [disabled]="!studentform.valid" value="Υποβολή" class="btn btn-success" />
    </form>
  </div>
  `
})
export default class Form3 {
    public student;


    constructor(private http: Http) {
51
        this.student = new Student(1, '', '', '', '', '','','','','');
52
        //this.student = new Student(1, 1, '', '');
53 54 55 56
    }

    onSubmit(studentform: any) {
        let headers = new Headers({
57 58
            //"Authorization": "Basic cmVzdHVzZXI6czNjckV0MFAwdWwwJA==", // encoded user:pass
             "Authorization": "Basic bmthdHNhb3Vub3M6emVtcmFpbWU=",
59 60 61 62
            "Accept": "*/*",
            "Access-Control-Allow-Credentials": "true",
            "Content-Type": "application/json",
            // "Content-Type": "text/plain",  // try to skip preflight
63 64 65
            //"X-CSRF-Token": "hVtACDJjFRSyE4bgGJENHbXY0B9yNhF71Fw-cYHSDNY"
            //"X-CSRF-Token": "fj1QtF_Z_p6kE19EdCnN08zoSjVfcT4Up-ciW6I0IG8"
            "X-CSRF-Token": "LU92FaWYfImfZxfldkF5eVnssdHoV7Aa9fg8K1bWYUc"
66 67 68 69 70 71 72 73 74 75 76 77 78
        });
        let options = new RequestOptions({ headers: headers, withCredentials: true });
        console.log(this.student);

        this.http.post(`${AppSettings.API_ENDPOINT}/entity/epal_student`, this.student, options)
    //    this.http.post('http://eduslim2.minedu.gov.gr/drupal/entity/epal_student', this.student, options)
            // Call map on the response observable to get the parsed people object
            .map((res: Response) => res.json())
            .subscribe(success => {alert("Η εγγραφή έγινε με επιτυχία"); console.log("success post")}, // put the data returned from the server in our variable
            error => console.log("Error HTTP POST Service"), // in case of failure show this message
            () => console.log("write this message anyway"));//run this code in all cases);
    }
}