I'm creating a small contacts application in AngularJS and want to retrieve data by calling a web service.
I created two projects in VS (one Website for the angularJS code and one Web Application for the web service code). I put them both under the same Solution.
This is my index.html markup:
<body ng-app="app" ng-controller="controller" ng-cloak>
<div class="header" ng-bind="header"></div>
<div class="content">
<!-- This content will switch -->
<ui-view></ui-view>
</div>...
My contacts.html markup, which goes inside ui-view tag:
<ul>
<li ng-repeat="c in contacts" >...
In JS I created the following service:
// Contacts Service
app.service("getContacts", function ($http, $q) {
// web-service path
var ws = "http://localhost:65123/";
var getContact = function () {
//create defer object
var contactsPromise = $q.defer();
$http.get(ws + "api/Contact")
.then(function (res) { contactsPromise.resolve(res);
}, function (err) { contactsPromise.reject(err); });
return contactsPromise.promise;
};
return
{ getContact: getContact }
});
and the following index Controller:
//index(root) Controller
app.controller("controller", function ($scope,getContacts) {
$scope.contacts = getContacts.getContact().then(function(res){ $scope.contacts = res; });
});
In my Web Application I created a single Model:
public class Contact
{
public string name;
public string phone;
public string mail;
public string address;
public static List<Contact> getContacts() {
// TODO retrieve Contacts from DB
List<Contact> c = new List<Contact>();
c.Add(new Contact() { name="aaaa", phone="111", mail="[email protected]",address="sdsd"});
c.Add(new Contact() { name = "bbbb", phone = "222", mail = "[email protected]", address = "sss" });
c.Add(new Contact() { name = "cc", phone = "444", mail = "[email protected]", address = "ggg" });
return c;
}
And its Controller:
namespace WebApplication1.Controllers
{
public class ContactController : ApiController
{
// GET: api/Contact
public List<Contact> Get()
{
return Contact.getContacts();
}
}
}
When I run the whole project (both Angular and WS) I get an ERR_CONNECTION_REFUSED error. What could be the problem here? Is something wrong with my ajax call using $http?
P.S. I allowed CORS in my Web Application.