2
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions,  } from '@angular/http';
@Injectable()
export class RoleService {
    headers = new Headers({"Content-Type": "application/json"});
     options = new RequestOptions({ headers: this.headers });
     constructor(private http: Http) {  }

    getRoleList(data) {
         return this.http.post('http://192.168.10.178:9080/role/getRole', data, this.options)
                .toPromise()
                .then(res => res.json().data)
                .then(data => { return data; });
    }
}

https://i.sstatic.net/nPiK8.png

Help me!! How to solve this ???

1
  • Please include a clear question, what are the issues, what is the result, etc ... Commented Jul 27, 2017 at 9:04

3 Answers 3

5

Try

export class RoleService {
  options: RequestOptions;

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

    this.options = new RequestOptions({ headers: headers });
  }

// ........
Sign up to request clarification or add additional context in comments.

6 Comments

error: Property 'append' does not exist on type 'Headers'.
@yanWu try changing headers: Headers; to headers: any;
wa~~~You're awesome!! Thanks
@yanWu If it works, then I think the Property 'append' does not exist on type 'Headers' error is the typescript compiler error.. Glad it works btw :)
sorry,I found another problem! import { Component, OnInit } from '@angular/core'; error:Cannot use imports, exports, or module augmentations when '--module' is 'none'. What happened??
|
0

Its might be fix your issue

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions,  } from '@angular/http';
@Injectable()
export class RoleService {
     constructor(private http: Http) {  }

    getRoleList(data) {
     let headers = new Headers({"Content-Type": "application/json"});
     let options = new RequestOptions({ headers: headers });
         return this.http.post('http://192.168.10.178:9080/role/getRole', data, this.options)
                .toPromise()
                .then(res => res.json().data)
                .then(data => { return data; });
}}

Comments

0

After the version of Angular 4.3, HttpClientModule was introduced along with HttpClient, HttpHeaders, HttpParams and HttpRequest and some of the method has been deprecated such as RequestOptions.

You can use HttpClientModule from @angular/common/http.

import { HttpClient, HttpRequest, HttpParams, HttpHeaders } from '@angular/common/http';
export class AppComponent {
    constructor(private http: HttpClient){}
    headers = new HttpHeaders({"Content-Type": "application/json"});

    callAPI(){
         return this.http.get(URL,{headers}).subscribe(data=>{
             console.log(data);
         });
    }
}

You can also refer :- https://angular.io/api/common/http/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.