2

I am trying to set content type to json for my http post call in angular js 2 service. but it is not setting to the json causing getting null values in the api parameter

import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
@Injectable()
export class DeviceService {
public headers: Headers;

constructor(private http:Http) {
    this.headers = new Headers();
    this.headers.append('Content-Type', 'application/json');
}

/*getAllUsers service to get data(users) from file or database*/
getDevice() {
    return this.http.get('assets/api/device.json').map(
        response => response.json()
    );
}

save(model) 
{
    return this.http.post('http://localhost:14/someapi/method',
        JSON.stringify(model),{headers: this.headers})
        .map(response => response.json());
}

But when making post call it always show content type as 'text/plain;charset=UTF-8' in firebug console.enter image description here

sachin kulkarni
  • 737
  • 1
  • 13
  • 26

1 Answers1

2

You forgot to import the Headers class:

import {Http, Headers} from 'angular2/http';

So you don't see any errors but the headers aren't included in the request...

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360