yan Wu
yan Wu

Reputation: 23

angular4 http headers , How to set up?

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 ???

Upvotes: 2

Views: 8852

Answers (3)

Dhiral Kaniya
Dhiral Kaniya

Reputation: 2051

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/

Upvotes: 0

samAlvin
samAlvin

Reputation: 1678

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 });
  }

// ........

Upvotes: 5

Tahsin Gül
Tahsin Gül

Reputation: 118

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; });
}}

Upvotes: 0

Related Questions