Hello, I am new to creating mobile apps with Ionic and Angular. Currently, I have a simple login form and a corresponding login button that triggers a controller. Here is the code snippet for the controller:
angular.module('starter.controllers', [])
.controller('LoginCtrl', function($scope, LoginService, $ionicPopup, $state) {
$scope.data = {};
$scope.login = function() {
LoginService.loginUser($scope.data.username, $scope.data.password)
.success(function(data) {
var alertPopup = $ionicPopup.alert({
title: 'Login HERE!',
template: 'HERE: your credentials!'
});
}).error(function(data) {
var alertPopup = $ionicPopup.alert({
title: 'Login failed!',
template: 'Please check your credentials!'
});
});
}
});
The above controller calls a service defined as follows:
angular.module('starter.services', [])
.service('LoginService', function($http,$q) {
return {
loginUser : function(name, pw) {
// Service implementation details here
}
}
})
I am successfully building and calling a RESTful API using the correct URL. Upon successful login, I receive a token ID as a string. In case of failure, I get an error message also as a single line.
After some research, I found out that a GET request expects an array of data while in my controller.js file, there's a variable declared as a String. The main issue I'm facing is that the .success method inside the controller is expecting an array instead of a String.
If anyone has suggestions on how to convert the response to an array or if it's possible to handle a single string response, please advise. Thank you!
UPDATE:
Below is the exception stack trace I encountered:
08-17 11:08:56.412: E/Web Console(6538): TypeError: Object #<Object> has no method 'success'
08-17 11:08:56.412: E/Web Console(6538): at Scope.$scope.login (file:///android_asset/www/js/controllers.js:36:12)
08-17 11:08:56.412: E/Web Console(6538): at $parseFunctionCall (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:21172:18)
08-17 11:08:56.412: E/Web Console(6538): at IonicModule.config.factory.element.onclick (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:53674:9)
08-17 11:08:56.412: E/Web Console(6538): at Scope.$get.Scope.$eval (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:23228:28)