Reputation: 1
I created RESTapi in Codeigniter which work fine so far. But today, returned data is something completely confused when i call API via angular. I'ts not first time i use angular, but now i can't understand where i doing mistake. I conclude that problem is in angular, because when i send request from "postman", result is what i expect.
My Angular Controller is this:
.controller('adminCtrl', function($http, $q, NgTableParams) {
var vm = this;
var deferred = $q.defer();
vm.selected = {};
sendHttpRequest = function(req) {
$http(req).success(function successCallback(response, status) {
deferred.resolve(response);
})
.error(function errorCallback(error) {
deferred.reject(error);
});
return deferred.promise;
};
vm.editGame = function(game) {
vm.selected = angular.copy(game);
};
vm.reset = function () {
vm.selected = {};
};
vm.getTemplate = function(game) {
if (game.gameId === vm.selected.gameId){
return 'edit';
}
else return 'display';
};
vm.save = function(idx) {
var updateData = {
method: 'POST',
url: "<?php echo base_url('api/updateResult'); ?>",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-API-KEY': "<?php echo $userInfo['API_Key']; ?>"
},
data: {
'gameId': vm.selected.gameId,
'homeSaves': vm.selected.homeSaves,
'awaySaves': vm.selected.awaySaves
}
};
console.log(updateData);
sendHttpRequest(updateData).then(function(x) {
console.log(x);
vm.games[idx] = angular.copy(vm.selected);
vm.reset();
}, function(error) {
swal({
title: "Error!",
text: error,
type: "error",
confirmButtonText: "OK!",
closeOnConfirm: true
}, function() {
vm.reset();
//window.location.href = "<?php echo base_url(); ?>";
});
});
};
var getData = {
method: 'GET',
url: "<?php echo base_url('api/result'); ?>",
headers: {
'X-API-KEY': "<?php echo $userInfo['API_Key']; ?>"
}
};
sendHttpRequest(getData).then(function(data) {
_.forEach(data, function(o) {
o.homeSaves = parseInt(o.homeSaves);
o.awaySaves = parseInt(o.awaySaves);
});
vm.games = data;
}, function(error) {
swal({
title: "Error!",
text: error,
type: "error",
confirmButtonText: "OK!",
closeOnConfirm: true
}, function() {
window.location.href = "<?php echo base_url(); ?>";
});
});
});
It is inline editor for some sports results. When APP start, we call sendHttpRequest(getData) and that's work well. If I try to simulate error by change url in request, error function will be returned, and "swal" will show error.
Problem is in vm.save function. If i call it whit all correct parameters, data would be saved in my DB without errors. But if I try to simulate errors by change url or force return error by RESTapi controller returned result is confused.
Instead of function(error), function(x) would be executed and print completely wrong result (result of vm.games) even REST return 405 Method Not Allowed in example where i change url to wrong OR 400/500 when i force REST controler to return error.
Upvotes: 0
Views: 79
Reputation: 1441
Try put var deferred = $q.defer(); inside sendHttpRequest function.
Or u are always using the promise that has been resolved by the first request.
Upvotes: 0