0

Would be grateful if someone could offer some insight or example with this.

I am building an Ionic 2 app which uses Angular 2. I have built apps in Ionic 1 and was able to intercept the http header globally

Ionic 1 was something like

 .config(function ($stateProvider, $urlRouterProvider, $httpProvider) {
    $httpProvider
        .interceptors.push(function ($injector, $q) {
        return {
            responseError: function (rejection) {
                // Probably not connected to the internet or connection problem.
                if (rejection.status == -1) {
                    return ErrorMessageAlert.connectionError();
                }

^ Basically if http failed to connect it would return -1 at which I would throw up an alert "connection error".

How can I do this in Ionic 2?

Right now my http request is something like:

page.ts

    getData(payload) {
    console.log(this.payload);
    let loading = this.loadingCtrl.create({ content: "Please wait..." });
    loading.present();
    this.pageService.getData(payload).subscribe(response => {
      loading.dismiss();
      this.data = response;
    });
  }

page-service.ts

  getData(payload) {

    let params: URLSearchParams = new URLSearchParams();

    for (let key in payload) {
      params.set(key.toString(), payload[key]);
    }

    return this.http.get(this.config.get('ApiBaseUrl'), { search: params })
      .map(res => res.json());
  }

How can I get that to throw an alert if the header rejection status is -1?

limit
  • 647
  • 2
  • 8
  • 27

0 Answers0