Unable to recover my webServices in AngularJS
-
Hello everybody, I'm writing you because I have a problem that blocks me in advancing of my project since few days. I created a WebApi which is recovered by my AngularJs module. Here is my controller in my WebAPI :
namespace API.Controllers
{
public class questionsController : ApiController
{
public static Lazy> questions = new Lazy>();
public static Question question = new Question();
public int pageLoad = 0;
public questionsController()
{questions.Value.Add(new Question { id = 1, libelle = "tettettetetetet" }); questions.Value.Add(new Question { id = 2, libelle = "poiuyterz" }); questions.Value.Add(new Question { id = 3, libelle = "udeuhdue" }); } // GET: questions public List GetQuestions() { return questions.Value; } // GET : questions/2 public IHttpActionResult getQuestion(int id) { question = questions.Value.First((p) => p.id == id); return Ok(question); }
When I test this service in my browser browser (localhost: .... / api / questions), I have questions who appear in XML format. However I can't seem to call these data in an HTML page with Angular Js module. Here is my HTML code:
<html ng-app>
<head>
<title>Questions</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script>
<script src="questionsController.js"></script>
</head>
<body>ID : {{ question.id }}
libelle : {{ question.libelle }}
id {{ questions.id }} ! libelle {{ questions.libelle }} !
</body>
</html>Here is my Angular JS code :
function toto($scope, $http) {
$http.get('http://localhost:57709/api/questions/3').success(function (data) {
$scope.questions = data;
})
.error(function () {
$scope.error = "An Error has occured while loading posts!";
$scope.loading = false;
});
}Here is the error that my browser returns m