1
I have a web API made in php that accesses a Mysql database that has return in JSON and I am using Ionic + Angularjs to develop an App that will use this API.
In my file services.js I created a Factory for each entity of my system, this way :
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 login 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 into the application with his email and password, after checking his identity the app must load the current event.(1) The first request, from login, is executed quickly, but the second (2) takes a long time to enter the then() method. I’ve tried using Success instead, but it’s the same way. I tested the service with exactly the same parameters in the Chrome app Advanced REST Client and the response time was around 600ms.
Is my server the problem? If the problem is with the server, why is it running much faster in ARC? Could be some problem related to one request being inside the other?
I tested the application by Google Chrome, Ionic View on mobile and native app for android, all versions find the same problem, but Chrome appears to run faster.
I’ve done something similar and it didn’t take long. I even left the code better when I used
chain
in requests. for example:login.verifica().then(function() { if (tal) return; else return Eventos.getCurrent(); }).then(function() { // Verifico aqui a promessa do Eventos });
, but 30 seconds is a strange thing. In case you already solved, post to us what was.– Kelwen Souza
@lordcheat To solve the problem I refigured the PHP method of the API by bringing all the information I needed in a single request. As the first requisition was executed quickly worked, but I did not find out why the delay, unfortunately.
– Raphael Castro Martin