Hello,
First of all, thanks you for the help, i’m sorry if my english isn’t perfect because i’m french.
So, i try to get a response from a HTTP page in PHP with this @ionic-native/http
My app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import { Accueil } from '../pages/accueil/accueil';
import { VotrePhotographe } from '../pages/photographe/photographe';
import { GaleriePage } from '../pages/galerie/galerie';
import { TarifsPage } from '../pages/tarifs/tarifs';
import { ContactPage } from '../pages/contact/contact';
import { EspaceClient } from '../pages/espace-client/espace-client';
import { GaleriePrive } from '../pages/galerie-prive/galerie-prive';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
/* Pour les email */
import { Camera } from '@ionic-native/camera';
import { EmailComposer } from '@ionic-native/email-composer';
/* Connexion SQL */
//import { HttpModule } from '@angular/http';
import { HTTP } from '@ionic-native/http';
@NgModule({
declarations: [
MyApp,
Accueil,
VotrePhotographe,
GaleriePage,
TarifsPage,
ContactPage,
EspaceClient,
GaleriePrive
],
imports: [
BrowserModule,
//HttpClientModule,
//HttpModule,
IonicModule.forRoot(MyApp),
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
Accueil,
VotrePhotographe,
GaleriePage,
TarifsPage,
ContactPage,
EspaceClient,
GaleriePrive
],
providers: [
StatusBar,
SplashScreen,
Camera,
EmailComposer,
{provide: ErrorHandler, useClass: IonicErrorHandler},
HTTP
]
})
export class AppModule {}
My espace-client.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { GaleriePrive } from '../galerie-prive/galerie-prive';
//import { EspaceClient } from '../espace-client/espace-client';
import { HTTP } from '@ionic-native/http';
/**
* Generated class for the EspaceClientPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-espace-client',
templateUrl: 'espace-client.html',
})
export class EspaceClient {
constructor(public navCtrl: NavController, public navParams: NavParams, private http: HTTP) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad EspaceClient');
}
login() {
this.navCtrl.push(GaleriePrive);
}
try() {
this.http.get('http://www.api.les-cherubins-photographie.fr/index.php?action=CheckUserExist&user=Nicolas', {}, {})
.then(data => {
console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);
})
.catch(error => {
console.log(error.status);
console.log(error.error); // error message as string
console.log(error.headers);
});
}
}
And my espace-client.html
<!--
Generated template for the EspaceClientPage page.
See http://ionicframework.com/docs/components/#navigation for more info on
Ionic pages and navigation.
-->
<ion-header>
<ion-navbar>
<!-- Ouvrir nav bar-->
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<!-- FIN DE NAV BAR -->
<ion-title>Espace Client</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding id="PageEspaceClient">
<ion-list>
<ion-item>
<ion-label stacked>Identifiant</ion-label>
<ion-input type="user" value=""></ion-input>
</ion-item>
<ion-item>
<ion-label stacked>Mot de passe</ion-label>
<ion-input type="password"></ion-input>
</ion-item>
</ion-list>
<button ion-button full class="Button" (click)="login()">Se connecter</button>
<button ion-button full class="Button" (click)="try()">TRY</button>
</ion-content>
My index.php (API)
<?php
ini_set('display_errors', 'on');
/* DATABASE */
$dbhost = "XXX";
$dbname = "XXX";
$dbuser = "XXX";
$dbpass = "XXX";
try {
$bdd = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
} catch(Exception $e) {
die('Erreur : '.$e->getMessage());
}
//$action = $_GET['action'];
if (isset($_GET['action'])) {
$action = $_GET['action'];
$nombre = strlen($_GET['action']);
if ($nombre >= 1) {
switch ($action) {
/* API CHECK IF THE USER EXIST */
case 'CheckUserExist':
if (isset($_GET['user'])) {
$username = $_GET['user'];
$query = 'SELECT username FROM users WHERE username = "'. $username . '"';
$query_res = $bdd->query($query);
$count= count($query_res->fetchAll());
if($count > 0){
$result = 'USER_EXIST';
} else {
$result = 'ERROR:USER_DO_NOT_EXIST';
}
} else {
$result = 'ERROR:USER_NOT_DEFINIE';
}
break;
/* BY DEFAULT IF NO THINGS IS DEFINE, IT RESULT AN ERROR */
default:
$result = 'ERROR:ACTION_UNKNOW';
}
} else {
$result = 'ERROR:ACTION_NULL';
}
} else {
$result = 'ERROR:ACTION_NOT_DEFINIE';
}
echo $result;
?>
So, the problem is, when i press the button “TRY” for see in if ionic can get the HTTP page the result is this:
[12:46:03] console.log: null
[12:46:03] console.log: null
[12:46:03] console.log: null
So i think the connection don’t work, but i don’t know why ?
I’m not realy good in php, and i’m learning ionic so, it’s hard ^^
Thx if you can help me