当我们使用angularjs从服务器访问数据时,我们得到一个CORSE-Error
我们的代码:
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”。
答案 0 :(得分:0)
可以通过在WebAPI的web.config文件中进行以下更改来解决此问题: 参考网址:enabling cross-origin resource sharing on IIS7
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
答案 1 :(得分:0)
我最近提出了一个问题,其中我收到了有关春季CORS问题的详细信息,请阅读Nikolay回答: Angular JS and Spring MVC @RestController - POST status 0