【问题标题】:Cross-Origin Request Blocked in Firefox跨域请求在 Firefox 中被阻止
【发布时间】: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


【解决方案1】:

可以通过在我们的 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>

【讨论】:

【解决方案2】:

我最近提出了一个问题,其中我收到了有关春季 CORS 问题的详细信息,请阅读 Nikolay 回答: Angular JS and Spring MVC @RestController - POST status 0

【讨论】:

    猜你喜欢
    • 2015-08-10
    • 2018-06-28
    • 2021-06-26
    • 2014-10-13
    • 2017-04-08
    • 2019-11-04
    相关资源
    最近更新 更多