In my application user need to enter an URL for getting authenticated (which will be used for making a POST request). But in case if user enter invalid host URL then how to report connection error etc. in the $http. Below is the code which perform the same.
login.html
<label class="item item-input">
<span class="input-label">Url</span>
<input type="text" placeholder="https://test.com" ng-model="loginData.crmUrl">
</label>
controller.js
angular.module('starter')
.controller('ActionsCtrl', ['$scope', '$ionicModal', '$http', '$state',
function ($scope, $ionicModal, $http, $state) {
. . .
// Perform the respective operations
$scope.doLogin = function () {
console.log("LOGIN user: " + $scope.loginData.username + " - PW: " + $scope.loginData.password);
console.log("Url is " + $scope.loginData.crmUrl);
$http({
url: $scope.loginData.crmUrl + '/modules/Mobile/api.php',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: 'username=' + $scope.loginData.username + '&password=' + $scope.loginData.password + '&_operation=login'
}).success(function (data, status, headers, config) {
if (data.success == true) {
console.log('Login is success');
//alert('We are able to login');
console.log(data);
$scope.closeModal(1);
$state.go('tabs.home');
} else {
alert('we failed');
}
}).error(function (data, status, headers, config) {
console.log(data);
});
. ..
})
Now for example user enter http://localhost:8080 (which is an invalid address) I need to report an error for asking for a correct URL.
How I can achieve the same.