Ionic-framework: Can't send an http request (Ionic 2 project)

Created on 22 Aug 2016  路  2Comments  路  Source: ionic-team/ionic-framework

Hello,
I try to create a class to have all common requests to call them from all other classes but i get this error message.

browser_adapter.js:84 EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in ./MyApp class MyApp_Host - inline template:0:0
ORIGINAL EXCEPTION: No provider for AppServices!
ORIGINAL STACKTRACE:
Error: DI Exception
at NoProviderError.BaseException [as constructor]

First of all, i created an app.service.ts and i included it from app.ts.This is app.service.ts

import { Injectable, Component} from '@angular/core';
import {Http,HTTP_PROVIDERS} from "@angular/Http";

@Injectable()
export class AppServices {
private http:Http;
constructor() {
}
static get parameters() {
return [[Http]];
}
getTranslations(API_URL:string){
var response =this.http.get(API_URL).map(res => res.json());
var translations:JSON;

    response.subscribe(
        data=> {
            translations = data.t;
            console.log(translations);
        },
        error => {
            console.log(error);
        },
        () => console.log("Completed")
    );
   // return Promise.resolve(translations);

}

}

this is app.ts

import { Component } from '@angular/core';
import { Platform, ionicBootstrap } from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { LoginPage } from './pages/login/login.component';
import { AppServices } from './app.service';
import {HTTP_PROVIDERS} from '@angular/http'

@Component({
template: ''
})

export class MyApp{

private rootPage: any;
constructor(private platform: Platform, services:AppServices) {

//this.rootPage = LoginPage;
platform.ready().then(() => {
  //services.getTranslations("https://yavrim-s2development.rhcloud.com/api/translation/en_US.js");
  // Okay, so the platform is ready and our plugins are available.
  // Here you can do any higher level native things you might need.
  StatusBar.styleDefault();    
});

}

}

ionicBootstrap(MyApp);

All 2 comments

Hello! Thanks for opening an issue with us! As this seems like more of a support question i will urge that you ask this question on our forum. Thanks for using Ionic!

Solved guys. I needed to define AppServices class on providers.
Example :

app.ts

import { Component } from '@angular/core';
import { Platform, ionicBootstrap } from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { LoginPage } from './pages/login/login.component';
import { AppServices } from './app.service';
import {HTTP_PROVIDERS} from '@angular/http'

@Component({
template: '',
providers: [ AppServices ]
})

export class MyApp{

private rootPage: any;

constructor(private platform: Platform, services:AppServices) {

//this.rootPage = LoginPage;
platform.ready().then(() => {
//services.getTranslations("https://yavrim-s2development.rhcloud.com/api/translation/en_US.js");
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
});
}

}

ionicBootstrap(MyApp);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexbainbridge picture alexbainbridge  路  3Comments

masimplo picture masimplo  路  3Comments

BilelKrichen picture BilelKrichen  路  3Comments

brandyscarney picture brandyscarney  路  3Comments

brandyscarney picture brandyscarney  路  3Comments