0

Hi I am trying to learn Spring RESTful Webservice with Angular js. I have Created a Simple Controller that returns Json Values

@RestController
public class TestController {

  @RequestMapping("/greeting")
  public GreetingModel greeting(@RequestParam(value="name",defaultValue="World")String user){
    
    GreetingModel greetingModel = new GreetingModel();
    greetingModel.setId("123");
    greetingModel.setContent(user);
    return greetingModel;
}

I get the Following Output on my chrome browser

{"id":"123","content":"World"}

I have created a html file as below

<html ng-app>
<head>
    <title>Hello AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
    <script src="js/hello.js"></script>
</head>

  <body>
    <div ng-controller="Hello">
        <p>The ID is {{greeting.id}}</p>
        <p>The content is {{greeting.content}}</p>
    </div>
  </body>
</html> 

hello.js

function Hello($scope, $http) {
return $http.get('http://localhost:8080/greeting').
    success(function(data) {
        $scope.greeting = data;
    });
}

I get the Following Output

The ID is

The content is

But when I Replace the the http.get in hello.js file

$http.get('http://localhost:8080/greeting')

with

$http.get('http://rest-service.guides.spring.io/greeting')

provide by Spring IO at https://spring.io/guides/gs/consuming-rest-angularjs/ I get the output

The ID is 581

The content is Hello, World!

Is there some issues using localhost

Community
  • 1
  • 1
  • 1
    Check the browser console and see what error you are getting. – Sanjay Rawat Mar 20 '16 at 16:37
  • My guess is it is a CORs issue. you are going to have to add a CORS filter to your spring-boot app – Nick Delaney Mar 23 '16 at 16:04
  • Hi Guys Sorry for the delayed Response I am getting the following error on my console log XMLHttpRequest cannot load http://localhost:8080/greeting. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403. – vishal sharma Mar 25 '16 at 04:30
  • @Nick Delaney Hi , I tried adding @CrossOrigin(origins = "http://localhost:8080") annotation but still getting the same error – vishal sharma Mar 25 '16 at 06:08
  • @vishalsharma Chrome will also disallow localhost cross origin requests by default, there is an extension for Chrome(if this is what you are using) https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en that will override that. – Nick Delaney Mar 25 '16 at 11:48
  • @NickDelaney Hi, The issue is resolved apparently the CrossOrgin annotations have issues. As of now the only way to fix this is by creating a filter https://jira.spring.io/browse/DATAREST-573. Thanks for your help. – vishal sharma Mar 25 '16 at 14:28
  • The Problem Can be solved at the below specified link. http://stackoverflow.com/questions/31724994/spring-data-rest-and-cors – vishal sharma Mar 25 '16 at 14:29

0 Answers0