简体   繁体   中英

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.stack.imgur.com/nPiK8.png

Help me!! How to solve this ???

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

// ........

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

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM