简体   繁体   中英

ReST and AngularJS

I've read up on many articles and posts regarding angular consuming ReST, and came away thinking that the below should work. But it does not.

<script src="scripts/rest.js"></script>
<div ng-controller="func">
    <p>content is {{response}}</p>
</div>

function func($scope, $http) {
    $http.get('http://localhost:8080/stuffs/api/thing/query').
        success(function(data) {
            $scope.response = data;
        }).
    error(function(error, status) {
        alert(status + error.message);
    });
}

The above gives 0 with an empty message. In an attempt to get something/anything, I was able to get something out of this mess:

$scope.responses.push(JSON.stringify($resource('http://localhost:8080/stuffs/api/thing/query', {}).get()));

This spits out {"$promise":{},"$resolved":false} which I understand to be from asynch behavior until resolved.

The above is a bit contrived, but I have attempted countless patterns. I feel like I'm missing some key detail here. How do I get this to work? Has something changed since these blogs were written?

FYI, direct invocation of the address from the browser responds with what it is supposed to, and curl -i gives a 200 response.

UPDATE

This static content here is now served from a local Jboss. Browser dev tools give a 200 with the response data of what I am expecting. Not sure how the authors of all these writeups got this to work, but starting from scratch to try a nicer pattern in hopes of resolving things. Still open to suggestions! Thanks for those who have responded thus far - very much appreciated.

This discussion got my brain churning thanks to you guys. This $http.get(...) actually does what its supposed to without any further complexity needed. When static content was served from the app-server and not the file system, everything came together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM