0

When we are accessing data from server using angularjs we are getting a CORSE-Error

Our code:

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;
    });
});

Controller-code:

@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);
}

The Error Message:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8081/NxtLife_Demo/getsurveyall. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

nirazul
  • 3,928
  • 4
  • 26
  • 46
Vasim Akram
  • 80
  • 1
  • 9

2 Answers2

0

This issue can be resolved by making the following change within the web.config file of our WebAPI : Reference URL:enabling cross-origin resource sharing on IIS7

   <system.webServer>
           <httpProtocol>
               <customHeaders>
                   <add name="Access-Control-Allow-Origin" value="*" />
               </customHeaders>
           </httpProtocol>
       </system.webServer>
Community
  • 1
  • 1
Ananthakumar
  • 323
  • 1
  • 14
0

I recently aked a question in which I received a detailed info about CORS issues in Spring, read Nikolay answer : Angular JS and Spring MVC @RestController - POST status 0

Community
  • 1
  • 1
pokemzok
  • 1,659
  • 1
  • 19
  • 29