I have a web API made in php that accesses a MySql database that has a return in JSON and I'm using Ionic + AngularJS to develop an application that will use this API.
In my services.js file I created a factory for each entity in my system, like this:
var apiUrl = "http://minhaUrl/";
angular.module('starter.services', [])
.factory('Login', function($http, $httpParamSerializerJQLike){
return {
verifica: function(emailParam, senhaParam){
return $http({
method: "POST",
url: apiUrl + "api/login/verificaLogin/",
data: $httpParamSerializerJQLike({
email: emailParam,
senha: senhaParam
}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
}
}
})
.factory('Eventos', function($http, $httpParamSerializerJQLike){
return {
getCurrent: function(){
return $http({
method: 'POST',
url: apiUrl + "api/eventos/listareventos/",
data: $httpParamSerializerJQLike(
{
param1: val1,
param2: val2
}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
}
}
});
In the controller.js file I implement the login in this way
.controller('LoginCtrl', function($scope, Login, Eventos){
//logar() é disparado no ng-click de um botão no HTML
$scope.logar = function(){
if($scope.Login.email && $scope.Login.senha){
Login.verifica($scope.Login.email, $scope.Login.senha)
.then(function(resp){
if(resp.data.sucesso){ //(1) Aqui ele demora menos de 1 segundo para entrar
localStorage.setItem("usuario", JSON.stringify(resp.data.login));
Eventos.getCurrent().then(function(respEventos){
$scope.Evento = respEventos.data.eventos[0]; //(2) Aqui demora uns 30 segundos pra entrar
});
}
else{
$scope.showAlert('E-mail ou senha incorretos!', 'Confirme as informações antes de logar.');
}
}, function(){
$scope.showAlert('Não foi possível completar a ação', 'Tente novamente mais tarde');
});
}
else{
$scope.showAlert('E-mail ou senha incorretos!', 'Confirme as informações antes de logar.');
}
}
});
First, the user logs in to the application with his email and password, after verifying his identity, the app should load the current event. (1) The first login request is executed quickly, but (2) second it takes too long to get into the then () method.
I've tried using success instead, but it stays the same way.
I tested the service with exactly the same parameters in the Advanced REST Client application and the response time was around 600ms.
Is the problem with my server? If the problem is with the server, why does it run much faster on ARC? Can it be a problem related to one request being inside the other?
I tested the application with Google Chrome, Ionic View on the phone, and the native app for android, all versions have the same problem, but Chrome seems to run faster.