working with Laravel back end and Angular front end. I have following code segments in Angular, employee.component.ts
import { Component, OnInit } from '@angular/core';
import { DataService } from 'src/app/service/data.service';
@Component({
selector: 'app-employees',
templateUrl: './employees.component.html',
styleUrls: ['./employees.component.css']
})
export class EmployeesComponent implements OnInit {
// tslint:disable-next-line: whitespace
// tslint:disable-next-line: typedef-whitespace
employees: any;
constructor(private dataService: DataService) { }
ngOnInit(): void {
this.getEmployeesData();
}
// tslint:disable-next-line: typedef
getEmployeesData() {
this.dataService.getData().subscribe(res => {
this.employees = res;
});
}
}
data.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private httpClient: HttpClient) { }
// tslint:disable-next-line: typedef
getData() {
return this.httpClient.get('http://127.0.0.1:8000/api/employee');
}
}
employee.component.html
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Salary</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let emp of employees">
<th scope="row">{{emp.id}}</th>
<th> {{emp.name}}</th>
<th>{{emp.email}}</th>
<th> {{emp.salary}}</th>
</tr>
</tbody>
</table>
and my Laravel api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('employee','EmployeeController@getEmployee');
Route::get('employee/{id}','EmployeeController@getEmployeeById');
Route::post('addEmployee','EmployeeController@addEmployee');
Route::put('updateEmployee/{id}','EmployeeController@updateEmployee');
Route::delete('deleteEmployee/{id}','EmployeeController@deleteEmployee');
but in my Angular html file not database employees data. not any errors here. both Angular and Laravel projects are running. how could I fix this problem?
Edit
My Error in Console
Access to XMLHttpRequest at 'http://127.0.0.1:8000/api/employee' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
127.0.0.1:8000/api/employee:1 Failed to load resource: net::ERR_FAILED
ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://127.0.0.1:8000/api/employee", ok: false, …}