I am currently experimenting with angularJS to interface with an API I developed. The root route of the API is set up to display information about the API in JSON format:
{
"Company": "Test Company",
"Version": "0.1"
}
When I use jquery's $.ajax()
method to access this data, everything works as intended and I receive the JSON response without any issues. However, when I try to fetch the data using $http.get() in angularJS, I encounter an error message in the console:
OPTIONS http://testapi.dev/ 401 (Unauthorized) angular.min.js:100
OPTIONS http://testapi.dev/ Invalid HTTP status code 401 angular.min.js:100
XMLHttpRequest cannot load http://testapi.dev/. Invalid HTTP status code 401
Below is the snippet of code from the index Controller that is responsible for fetching the data:
var index = angular.module('index', ['$strap.directives']);
index.factory('infoFactory', function ($http) {
var info_url = 'http://testapi.dev/';
var login_url = info_url + 'login';
var info = {};
return {
info: info,
getInfo: function () {
return $http.get(info_url);
}
};
});
index.controller('indexCtrl', function indexCtrl ($scope, infoFactory) {
infoFactory.getInfo().success(function (data) {
if (data.Success) {
$scope.info = data.Data;
infoFactory.info = data.Data;
} else {
alert(data.Message);
}
});
});