Angular 5 - HttpClient Post Service not working

I am having trouble getting a service with httpClient post and I'm confused with different versions and depreciated code.

Here is what I have:

// dataService.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable()
export class DataService {

  constructor(private http: HttpClient) {}

  insertData() {
    const body = JSON.stringify({firstName: 'Joel', lastName: 'Smith333'}),{headers: headers});
    return this.http.post('http://myurl.somefile.php', body, httpOptions);
  }

}

// app.component.ts
import { Component } from '@angular/core';
import { DataService } from './services/data.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {

  constructor(private dataService: DataService) {}

  myInsert() {
    if (this.dataService.insertData()) {
      alert('Data Inserted Successfully');
    }

  }

}

Open in new window


// app.component.html
<div class="btn btn-default" (click)="myInsert()">Click Me</div>

Open in new window


The issues are coming from the service...

httpOptions and the insertData functions has errors all over.

Can anyone please help to get this working with Angular 5 please?
LVL 1
error2013Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
you've a typo line 17, you've a right parenthesis misplaced :
    const body = JSON.stringify({firstName: 'Joel', lastName: 'Smith333'}),{headers: headers});
instead
    const body = JSON.stringify({firstName: 'Joel', lastName: 'Smith333'},{headers: headers});
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
error2013Author Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Angular

From novice to tech pro — start learning today.