My component file:
import { Component , OnInit } from '@angular/core';
import {Http , Response} from '@angular/http';
import { FormGroup , FormBuilder, Validators } from '@angular/forms';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-banner' ,
templateUrl:'./banner.component.html' ,
})
export class BannerComponent
{
url: string = "http://localhost/demos/items.php";
data = [];
complexForm : FormGroup;
constructor(fb: FormBuilder , private http:Http) {
this.complexForm = fb.group({
'firstName' : [null, Validators.required]
})
}
onSubmit(value: any ){
this.http.post(this.url ,JSON.stringify(value) ).map((res:Response) => res.json() ).subscribe(ans => {
this.data = ans;
console.log(this.data);
});
}
}
In angular2 I got an error like this:
Failed to load http://localhost/demos/items.php: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.