繁体   English   中英

angular4 http headers,如何设置?

[英]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

帮我!! 如何解决这个问题?

尝试

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

// ........

它可能是解决您的问题

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

的角4.3版本之后, HttpClientModule用一起引入HttpClientHttpHeadersHttpParamsHttpRequest和一些方法已被弃用如RequestOptions

您可以从@angular/common/http使用HttpClientModule

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

您还可以参考: -https : //angular.io/api/common/http/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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