Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple angularJS GET function with localhost not working

I have a very simple Spring Rest backend that returns JSON data. The restful URL is working in my browser like this:

http://localhost:8080/abc/runlist

It returns data like so:

[
  {"stock_num":"KOH19","damage":"Toronto (Oshawa)"},
  {"stock_num":"AZ235","damage":"Toronto (Oshawa)"},
   ...
]

I have an independent html page that is not part of my web app. I just want to test to see if my angular code is getting the data and then looping through it.

<!DOCTYPE html>
<html>
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <body>

    <div ng-app="myApp" ng-controller="customersCtrl">

    <ul>
      <li ng-repeat="x in names">
        {{ x }}
      </li>
    </ul>

    </div>

    <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://localhost:8080/abc/runlist")
    .success(function (response) {$scope.names = response.records;});
    });
    </script>

    yo yo

    </body>
</html>

Why is it not working? I came across something in Spring where you need to implement something called CORS. I did that like so but still nothing returned:

@Component
public class SimpleCORSFilter implements Filter {
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain  
    chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, 
    DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        chain.doFilter(req, res);
    }
    public void init(FilterConfig filterConfig) {}
    public void destroy() {}
}
like image 721
logixplayer Avatar asked May 26 '15 17:05

logixplayer


3 Answers

Try something like:

js:

var app = angular.module('myApp', []);
 app.controller('customersCtrl', function($scope, $http) {
  $http.get("http://localhost:8080/abc/runlist")
           .success(function (response){
              $scope.names = records;
           });
});

html:

<ul>
  <li ng-repeat="x in records">
    {{x}}
  </li>
</ul>

more params html:

<ul>
  <li ng-repeat="x in records">
    {{x.myName}}
    {{x.myNumber}}
  </li>
</ul>

Hope I've been helpfull.

like image 123
AndreaM16 Avatar answered Oct 23 '22 00:10

AndreaM16


Try putting return in front of your $http.get

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    return $http.get("http://localhost:8080/abc/runlist").success(function (response) {
        $scope.names = response;
    });
});
</script>

then in your view use the dot notation to refer to the properties you want to display e.g.

{{x.stock_num}}
like image 1
Grant Avatar answered Oct 23 '22 00:10

Grant


Got it! There were 2 fixes:

Firstly I had to implement the CORS filter and class in order not get this error:

org.apache.catalina.connector.ClientAbortException: java.io.IOException: An established connection was aborted by the software in your host machine
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:393)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:426)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:339)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:418)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:406)

Secondly, a warning for example copiers! I had copied the simple example above from W3Schools, a great tutorial site as such:

$http.get("http://localhost:8080/abc/runlist")
.success(function (response) {$scope.names = response.records;});

Note the .records at the end of response. This was not needed. When I took it off, it worked.

like image 1
logixplayer Avatar answered Oct 23 '22 00:10

logixplayer