【问题标题】:Send HTTPS requests in Angular / Ionic在 Angular / Ionic 中发送 HTTPS 请求
【发布时间】:2019-03-05 22:10:26
【问题描述】:

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

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 说我必须在最后添加/,但我不确定是否仍然如此,等等。

您好。

【问题讨论】:

  • 你需要使用 rxjs 并从那里调用 apis
  • 您能详细说明一下吗?以一些代码为例来回答?等
  • @OiciTrap 是您的客户端应用程序也托管在https 上吗?
  • 您提供的链接适用于 Angular 1.x。如果它们在同一个端口上,只需将 API url 中的 http 替换为 https 就足够了
  • @PankajParkar 客户端只是一个移动应用程序

标签: angular ionic-framework angular-http


【解决方案1】:
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;

}

你必须改变它

【讨论】:

猜你喜欢
  • 2016-08-31
  • 2017-11-07
  • 2018-10-03
  • 2018-09-11
  • 1970-01-01
  • 2019-04-08
  • 2018-11-24
  • 1970-01-01
  • 2019-05-18
相关资源
最近更新 更多