简体   繁体   中英

Angular http post with REST

I'm still learning angular and am trying to use a post method with json. Heres what the json looks like

  [{"race":{"date":"Thu 6th Apr", "raceName" :"final race" ,"horses":[{"horse":{"age":"9yo" "horseName":"The New One"Davies", {"distanceWins":0,"horse":{ "age":"9yo" "horseName":"Buveur D'Air" , {"distanceWins":0,

I'm able to get races using an angular controller linking to a html page as seen below

   angular.module('horseApp.RacesController',[]).
  controller('RacesController', function ($scope, $stateParams, $http, $state){

$http.get('restful-services/api/getAllRaces')
    .success(function(data, status) {
        $scope.race = data;
    })
    .error(function (error) {
        alert('An error occurred');
    });

<table>
<tr>
    <th>Race</th>
    <th>Date</th>

</tr>
<tr ng-repeat="r in race" ng-click="showRace(r)">
    <td>{{r.race.raceName}}</td>
    <td>{{r.race.date}}</td>

</tr>

</table>

what I'm trying to do is when when someone clicks a race it will display a new html page with all the horses in that race. Here's what I've tried`. In that controller that i posted above I added this method

$scope.showRace = function (race) {
    console.log(race.horse,'HORSES');
    $state.go('specificRace',{
        horse:race.horse
    })
}

In a new controller I added this post method with a html which was to display the horses when the race was clicked

angular.module('horseApp.ProfileController',[]).
controller('ProfileController', function ($scope, $stateParams, $http) {


var horsename = $stateParams.horsename;
console.log(horsename);
$http.post('restful-services/horse/getHorseByHorsename', horsename)
    .success(function(data, status) {
        $scope.horse = data;



    })
    .error(function (error) {
        alert(error);
    });
    });   


<table>
<tr>
    <th>Horse Name</th>


</tr>
<tr>
    <td>{{race.horse.horseName}}</td>

</tr>
</table>

Currently I'm getting an undefined error on line 6 in the ProfileController and a 500() error post http://localhost:8080/restful-services/horse/getHorseByHorsename from my RestApi class here

 @POST
 @Path(value="/getHorseByHorseName")
 @Produces(value={"application/json"})
 public Horse getHorseByHorseName(String horsename) {
    return horseDAO.findByHorsename(horsename);
 }

Sorry for the length of the post, any help is much appreciated.

  1. In your server side code, /getHorseByHorseName should be a GET call not POST

  2. You are passing host name under horse key $state.go('specificRace',{horse:race.horse}) but you are expecting param horsename which is the reason for undefined .

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