I have a spring controller which looks like this:
@RestController
public class RaceResultController {
@Autowired
RaceResultImpl raceResultImpl;
@GetMapping("/race")
public ModelAndView hello(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
List<RaceTopResult> raceTopResultList = raceResultImpl.getResults();
ObjectMapper o = new ObjectMapper();
model.addAttribute("races", raceTopResultList);
return new ModelAndView("race");
}
}
Then I have some embedded angular code in my view race.html:
<head>
<title>F1 RESULTS 2018</title>
<script
src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> .
</script>
<script
src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js"></script>
</head>
<body>
<header>
<h1>F1 RESULTS 2018</h1>
</header>
<div ng-app="raceApp" ng-controller="raceCtrl">
<table id="my_table" border = "1">
<tr ng-repeat="race in races">
<td>{{ race.driver }}</td>
<td>{{ race.team }}</td>
</tr>
</table>
</div>
</body>
<script>
var app = angular.module('raceApp', []);
app.controller('raceCtrl', function($scope, $http) {
alert("mini");
$http({
url: "/race",
method: "GET",
dataType: 'json'
}).then(function successCallback(response) {
alert("hi")
$scope.races = response.data;
}, function errorCallback(response) {
alert("bye");
$scope.error = response.statusText;
});
});
</script>
When I'm hitting /race
url in the browser, it always goes to the error callback block, even though when I test my controller separately. I can see it returns data from the service, but I cannot get the data in angular http response. Please help.
Thanks