I am new to angularjs and resently began to try consuming RESTful web services with angularjs. In my Spring MVC application I have a controller:
@Controller
@RequestMapping(value = "/exam/{examName}")
public class SomeController {
@Autowired
private QuestionService questionService;
@RequestMapping(value = "/{examId}", produces = "application/json; charset=UTF-8")
@ResponseBody
public String getExam(@PathVariable int examId) {
List<Question> questions = questionService.find(Question.class, "examId", examId);
return JSONUtil.convertObjectToJSON(questions); //A json array is returned
}
}
This controller returnes following JSON array:
[
{
"id": 1,
"examId": 2,
"text": "1. Eyniköklü sözlər cərgəsini müəyyənləşdirin.\r\n"
},
{
"id": 2,
"examId": 2,
"text": "2. Sait səslərə aid düzgün fikiriəri seçin.\r\n\n\n\n\n\n1. Hava axını ağız boşluğunda heç bir maneəyə rast gəlmir.\r\n"
},
{
"id": 3,
"examId": 2,
"text": "3. Asılı tərəfi əsl Azərbaycan sözü ilə ifadə olunmuş söz birləşmələrini göstərin.\r\n"
},
{
"id": 4,
"examId": 2,
"text": "4. \"Amma sən lap ağ elədin ha\" cümləsində köməkçi nitq hissələrini ardıcıllıqla göstərin.\r\n"
},
{
"id": 5,
"examId": 2,
"text": "5. Hansı söz birləşməsinin asılı tərəfi aşağıdakı sxemə uyğundur?\r\n"
}
]
And here is my html file, that consumes this web service with angularjs:
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
<div ng-app="" ng-controller="customersController">
<div ng-repeat="x in names">
{{ x.text }} <br>
</div>
</div>
<script>
function customersController($scope,$http) {
$http.get("http://localhost:8086/QuizNoqteAz/exam/abituriyent/2")
.success(function(response) {$scope.names = response;});
}
</script>
</body>
</html>
The web service works correctly, I have tested it with Postman Rest Client. Angular js isn't working. The html file produces a blank page and nothing is shown.
Any help is highly appreciated.
console.log(response)inside the$http.getto see what's actually being returned.