samedi 13 mars 2021

unable to insert data in Angular to Laravel API

working with Angular fronted and Laravel back-end API with MySQL. anyway using Angular form trying to insert data to back-end.I have following codes in Angular, employees.component.ts

// tslint:disable-next-line: typedef
  insertData(){
    this.dataService.insertData(this.employee).subscribe(res => {
      console.log(res);
    });
  }

data.service.ts

// tslint:disable-next-line: typedef
  insertData(data){
    return this.httpClient.post('http://127.0.0.1.8000/api/addEmployees', data);
  }

employees.components.html

<form (ngSubmit)="insertData()">
    <div class="form-group">
        <label for="name">Name</label>
        <input type="text" name="name" class="form-control" [(ngModel)]="employee.name">
    </div>
    <div class="form-group">
        <label for="name">Email</label>
        <input type="text" name="email" class="form-control" [(ngModel)]="employee.email">
    </div>
    <div class="form-group">
        <label for="name">Salary</label>
        <input type="text" name="salary" class="form-control" [(ngModel)]="employee.salary">
    </div>
    <button class="btn btn-dark btn-sm mt-4">Submit</button> 
</form>

and laravel api.php

Route::post('addEmployee','EmployeeController@addEmployee');

EmployeeController.php

public function addEmployee(Request $request){
        $employee = Employee::create($request->all());
        return response($employee, 201);
    }

but when I go to insert data using form following errors are coming in the console zone-evergreen.js:2845 POST http://127.0.0.1.8000/api/addEmployees net::ERR_NAME_NOT_RESOLVED ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://127.0.0.1.8000/api/addEmployees", ok: false, …}

how could I fix this problem here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire