2

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.

7
  • Have you tried defining a module to store the controller in? Commented Dec 21, 2014 at 23:56
  • I am new to angularjs and didn't use modules yet. The method that I use here is described in w3schools: w3schools.com/angular/tryit.asp?filename=try_ng_customers_json And I just don't get why my json is not parsed by angularjs. Commented Dec 22, 2014 at 0:03
  • You could try doing a console.log(response) inside the $http.get to see what's actually being returned. Commented Dec 22, 2014 at 0:09
  • XMLHttpRequest cannot load localhost:8086/QuizNoqteAz/exam/abituriyent/2. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. Commented Dec 22, 2014 at 0:36
  • 1
    You have to enable CORS (Cross Origin Requests) on your Spring MVC. Here's a guide: spring.io/guides/gs/rest-service-cors Commented Dec 22, 2014 at 7:48

1 Answer 1

2

To see what's wrong you can output the response to the console by using console.log(response) and look in the browser's console. This will tell you what is being returned from the server.

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

is because of CORS is not enabled for the server. This guide will tell you how to do it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.