Javascript 带REST的角度http post

Javascript 带REST的角度http post,javascript,java,angularjs,json,rest,Javascript,Java,Angularjs,Json,Rest,我仍在学习angular,并尝试使用json的post方法。下面是json的样子 [{"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

我仍在学习angular,并尝试使用json的post方法。下面是json的样子

  [{"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,
我能够使用一个链接到html页面的角度控制器获得比赛,如下所示

   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>
在一个新的控制器中,我添加了一个带有html的post方法,在点击比赛时显示马

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>
很抱歉文章太长,非常感谢您的帮助

  • 在服务器端代码中,
    /getHorseByHorseName
    应该是
    GET
    callnot
    POST

  • 您正在
    horse
    $state.go('specificRace',{horse:race.horse})下传递主机名
    ,但您需要参数
    horsename
    ,这是
    未定义的原因


  • 发送到服务器的数据需要密钥/值对…发送的只是值
    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>
    
     @POST
     @Path(value="/getHorseByHorseName")
     @Produces(value={"application/json"})
     public Horse getHorseByHorseName(String horsename) {
        return horseDAO.findByHorsename(horsename);
     }