My web-based project is fully written in jQuery and JavaScript. On the client side, I am calling RESTful webservices via AJAX like this:
$.ajax({
type: 'GET',
timeout: 1000000000,
headers: { 'Access-Control-Allow-Origin': '*' },
url: serverName + '/getAlerts/',
data: {},
dataType: "text",
success: function (data) {
$scope.alerts = JSON.parse(data);
$scope.$apply();
},
error: function (data) {
alert(errServiceCall);
}
});
In the server side, the RESTful webservices are created in Spring. An example function looks like this:
@RequestMapping(value = "/services/getAlerts", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
@ResponseBody
public String getAlerts(HttpServletRequest request) throws JSONException, IOException, SQLException {
return "hello it's me";
}
However, when I try to call that function on Chrome, I encounter the following error:
(index):374 Refused to connect to '' because it violates the following Content Security Policy directive: "connect-src 'self'".
If I manually enter the RESTful URL () in the Chrome address bar, it returns the result successfully.
I believe this issue is related to CORS (Cross-Origin Resource Sharing), so I included the code:
headers: { 'Access-Control-Allow-Origin': '*' },
In my AJAX code block. However, this did not solve the problem.
To address the CORS problem, I attempted to add the @CrossOrigin parameter above the function on the server side like this:
@CrossOrigin
@RequestMapping(value = "/services/getAlerts", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
@ResponseBody
public String getAlerts(HttpServletRequest request) throws JSONException, IOException, SQLException {
return "hello it's me";
}
But in doing so, I encountered the following error at compile time (Java version 1.6 + spring version 4.3.12):
annotation org.springframework.web.bind.annotation.CrossOrigin is missing < clinit>
How can I effectively resolve this CORS issue?
P.S: The process works perfectly in Internet Explorer when I enable CORS from Internet Options -> Security -> Custom Level -> Miscellaneous -> Access data sources across domains.