I'm currently attempting to make multiple GET requests to my own REST API built in Java using Spring. Out of every 10 times, both requests are handled successfully 3 times and 7 times one of the HTTP requests fails with a 'No Access-Control-Allow-Origin header is present on the requested resource' error. My REST Controller code snippet looks like this:
@RestController
@CrossOrigin
@RequestMapping("/person")
public class PersonRestController {
@Autowired
ActivityTracker tracker;
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void addPerson(@RequestBody Person person){
tracker.addPerson(person);
}
// Other Endpoint Methods...
}
My Angular application's structure goes as follows:
angular.module('trackerApp', ['ngCookies', 'ui.router', 'pascalprecht.translate'])
.config(function($stateProvider, $urlRouterProvider, $translateProvider){
// Configuration settings
})
.controller('personDetailController', function($http, $stateParams){
// Angular controller logic
});
Most of the time, both GET requests are processed without any issues and show the expected results. However, sometimes only one request fails and returns an error message stating:
XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 500.
It's puzzling why sometimes both requests are successful while other times only one of them fails?
I have also made attempts such as using @ResponseBody, setting headers, and configuring XML setup.