从HTML表单获取值返回Null with REST

时间:2017-05-23 11:34:42

标签: javascript java angularjs rest

我无法将html表单中的数据作为参数从我的java rest方法中获取。 这是我的html和角度代码:

var app=angular.module("myApp",[]);
app.controller("mycon",function($scope,$http){
    console.log("entered here1");
    $scope.test={};
    $scope.add = function() {
        console.log("entered here2");

    var json=JSON.stringify($scope.test);
    console.log($scope.test);
    console.log(json);

   $http.get("rest/xyz/role", 
           {
                data:json
           }
   ).success(function(data, status, headers, config) {
        $scope.data = data;
    }).error(function(data, status, headers, config) {
        alert("error");
    })





}});
<body ng-app="myApp" ng-controller="mycon">

<center>
<h2> Login Here! </h2>
<hr/>
    <form >

        <table>
        <tr>
            <td>Signum Id:</td> 
            <td><input type="text" ng-model="test.sig"/></td>

        </tr>

        <tr>
      <button class="btn waves-effect waves-light" type="submit" ng-click="add()" name="action"> Submit <i class="material-icons right">send</i>
        </tr>
        </table>


    </form>
</center>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/js/materialize.min.js"></script>


</body>

Rest代码在这里:

@GET
@Path("/role")
@Consumes({ MediaType.APPLICATION_JSON})
//@Consumes("text/plain")
@Produces({ "application/json" })
public String list(String s) {
    System.out.print("Entered here ");

    System.out.print(s);

    return null;
}

我希望表单中的signum字段作为rest方法中的参数传递。     我得到的输出是null。

1 个答案:

答案 0 :(得分:0)

使用http.post:

 $http.post("rest/xyz/role", {
                data:json
           } ...