无法在标头中发送数据以在php中授权请求

时间:2018-04-20 09:13:38

标签: http-headers angular5 angular-services

我在cli中收到错误消息:

Type 'Headers' has no properties in common with type 'RequestOptionsArgs'

。但是,代码执行。我使用的版本是5,如package.json中所示。我无法找到一个适合我需要的好的http示例。我想在header部分发送一些参数,并在php中进行授权。这是服务代码: user.service.ts

import { Observable } from 'rxjs/Observable';
import { Customer } from './../models/customer';
import { Injectable } from "@angular/core";
import { Http, Response, Headers, RequestOptions } from "@angular/http";

import "rxjs/add/operator/map";
import { apiServicesURL,appServicesURL } from "../constants/globals";
import { HttpClient, HttpHeaders } from "@angular/common/http";


@Injectable()
export class UserService {

//headers : Headers ;
constructor(private http: Http) {
}

getCustomerInfoo(custId): Observable<Customer> {
    //cust.push({token : localStorage.getItem('token')}); 
    console.log(apiServicesURL + 'getCustomers');
    let token = localStorage.getItem('token');
    let api_token = localStorage.getItem('api_token');
    let email = localStorage.getItem('email');
    let headers = new Headers({ 'Content-Type': 'application/json' });
   // let options: RequestOptions = new RequestOptions({ headers: headers });

    return this.http.post(apiServicesURL + 'getCustomers', JSON.stringify({ customer_id: custId, token: api_token, email: email }), headers  ).map((response: Response) => {
        // login successful if there's a jwt token in the response
        return <Customer>response.json();


    });

}


}

任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

如果您使用的是角度5,请尝试以下代码并将RequestOptionsProvider写入module.ts文件

import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HTTP_INTERCEPTORS } from '@angular/common/http';
import { AuthService } from '@app/core/services/auth.service';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class DefaultRequestOptionsService implements HttpInterceptor {

  constructor(private auth: AuthService) { }
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // Get the auth header from the service.
    // const authHeader = this.auth.getAuthorizationHeader();
    // Clone the request to add the new header.
    // const authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
    const authReq = req.clone({ headers: req.headers.set('Content-Type', 'application/json') });
    // Pass on the cloned request instead of the original request.
    return next.handle(authReq);
  }

}
export const RequestOptionsProvider = { provide: HTTP_INTERCEPTORS, useClass: DefaultRequestOptionsService, multi: true };