【问题标题】:Getting value from HTML form returns Null with REST从 HTML 表单获取值返回 Null 和 REST
【发布时间】:2017-10-23 08:10:32
【问题描述】:

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

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>

其余代码放在这里:

@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;
}

我希望表单中的符号字段作为参数传递给 rest 方法。 我得到的输出为空。

【问题讨论】:

  • return null 在你的后端函数中
  • conole.log 语句的输出是什么?

标签: javascript java angularjs rest


【解决方案1】:

使用http.post:

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

【讨论】:

  • 如果 OP 应该使用$http.post 他/她还必须更改后端的注释
  • 当然可以,因为get请求不能取json数据。
  • 我现在正在获取 json 数据。但它是字符串格式,我如何访问该 json 数据的 sig 字段。
  • 使用 JSON.parse()。例如 var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
  • 如何将表单中的数据作为字符串传递给我的休息服务。我希望传递值。字符串 s 具有 json 格式的数据
猜你喜欢
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多