【发布时间】:2016-01-29 05:22:48
【问题描述】:
当我们使用 angularjs 从服务器访问数据时,我们会遇到 CORSE 错误
我们的代码:
var app = angular.module("app", []);
app.controller("Ctrl", function($scope, $http) {
$scope.survey = true;
$scope.question = false;
$http.get("http://localhost:8081/NxtLife_Demo/getsurveyall").success(function(data) {
$scope.surveys = data;
});
});
控制器代码:
@RequestMapping(value = "/getsurveyall", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String getSurveyAll() throws JsonProcessingException {
Set<SurveyRecords> records = new HashSet<SurveyRecords>();
records.addAll(surveyService.getSurveyAll());
return new ObjectMapper().writeValueAsString(records);
}
错误信息:
跨域请求被阻止:同源策略不允许读取 http://localhost:8081/NxtLife_Demo/getsurveyall 的远程资源。 (原因: CORS 标头“Access-Control-Allow-Origin”缺失)。
【问题讨论】:
标签: javascript java asp.net angularjs