在Angular / Ionic中发送HTTPS请求

时间:2018-09-30 17:16:39

标签: angular ionic-framework angular-http

我有一个应用程序,其中发出的请求类似于您在以下代码中看到的请求:

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class ResourcesApiProvider {
  public direccion = my_url_app;

  constructor(public http: Http) {
  }

  getCars(car_id) {
    let repos = this.http.get(this.direccion + 'api/cars.json' + '?car_id=');

    return repos;
  }
}

我目前已经通过HTTP和HTTPS响应的API,但是我想将所有通信更改为HTTPS,所以我在想...我该怎么做,以便Angular / Ionic应用程序将加密的请求发送到API ?,在执行任务public direccion = my_url_app时仅使用API​​的HTTPS URL就足够了吗?

我问是因为some answers from here说我必须在末尾添加/,但是我不确定是否仍然如此,等等。

问候。

1 个答案:

答案 0 :(得分:0)

import { Injectable } from '@angular/core';

import { Model } from './Model.model';

import { Http, Headers, Response, RequestOptions } from '@angular/http';

import { Observable } from 'rxjs/Rx';

import { LoginService } from '../../core/auth/login/login.service';

import { environment } from '../../../environments/environment';

@Injectable()

export class Service {

    requestOptions: RequestOptions;

    constructor(private http: Http, loginService: LoginService){
        const headers = new Headers({'Authorization': loginService.getToken()});
        this.requestOptions = new RequestOptions({headers : headers});
    }

    getEventsType(): Observable<any[]> {
        return this.http.get(`${environment.apiUrl}/events`, this.requestOptions)
            .map( mapAny );
    }

}

function mapAny(response:Response): any[] {

    // The response of the API has a results
    // property with the actual results

    return response.json().data;

}

您必须进行更改

enter image description here