简体   繁体   English

我没有使用angularjs将数据传输到我的宁静Web服务上

[英]I am not getting data on to my restful web service using angularjs

This is my angular js code in which i want to send data in Json format to my restful web service written in java. 这是我的Angular js代码,我想将Json格式的数据发送到用Java编写的静态Web服务。 It makes call to the webservice but i don't get any data. 它可以打电话给网络服务,但我没有任何数据。

    <script>
        var app = angular.module("myPost", []);
        app.controller("myControl", function ($scope, $http) {

            $scope.title = "",
                    $scope.rating = "",
                    $scope.feedback = "",
                    $scope.role = "";

            $scope.send = function (title, rating, feedback, role) {
                var feed = {
                    title: title,
                    rating: rating,
                    feedback: feedback,
                    role: role
                };
                var head = {'Content-Type': 'application/json'};
                $http({
                    method: 'POST',
                    url: 'myurl',
                    data: feed,
                    headers: head
                }).success(function (data, status, headers, config) {
                    $scope.users = data;
                }).error(function (data, status, headers, config) {
                    $scope.error = status;
                });
            };
        });
    </script>
</head>
<body ng-app="myPost" ng-controller="myControl">
    <form action="">
        Title: <input type="text" name="title" value="" ng-model="title" />{{title}}<br>
        Rating:<br> <select name="rating" ng-model="rating">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
        </select><br>
        Feedback: <input type="text" name="feedback" value="" ng-model="feedback"/><br>
        <input type="hidden" name="type" value="article" ng-model="role" />
        <input type="submit" ng-click="send(title, rating, feedback, role)" value="submit" />

This is my restful Web Service where I want the data. 这是我需要数据的宁静的Web服务。

@Path("/database")
public class database {

    @Path("/insert")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String insert(@QueryParam("title") String title,
            @QueryParam("rating") int rating,
            @QueryParam("feedback") String feedback,
            @QueryParam("role") String role) {
        Data d = new Data();
        d.setTitle(title);
        d.setRating(rating);
        String response = new Gson.toJson(d);
    }

return response; 返回响应; } }

You should not use @QueryParam since you don't use the GET method but the POST method. 您不应该使用@QueryParam因为您不使用GET方法,而是使用POST方法。 With POST , you don't transmit query params with the url but you transmit data enclosed in the body of the request. 使用POST ,您不会使用url传输查询参数,而是传输包含在请求正文中的数据。

So from the rest controller side, to retrieve the information you can use as parameter your Data class if it contains the fields and types of fields matching with sent parameter names and values. 因此,从其余控制器方面,如果Data类包含与发送的参数名称和值匹配的字段和字段类型,则可以将您用作Data类的参数用作信息。

@Path("/insert")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String insert(Data data) {
   ...
}

You should do this: 你应该做这个:

$scope.users = data;

instead of this: 代替这个:

$scope.users = status;

The status is only the response code of the request. 状态仅是请求的响应代码。 Hope this helped) 希望这有所帮助)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Web服务从xml文档中获取数据时,出现空指针异常 - I am getting null pointer exception when i get the data from xml document using web service 使用AngularJS消费RESTful Web服务 - Consuming a RESTful Web Service with AngularJS 如何在angularjs中使用$ resource消耗Spring Restful Web服务 - how to consume spring restful web service using $resource in angularjs 为什么我的参数没有传递到RESTful Web服务? - Why my parameters are not getting passed to the RESTful web service? 当我尝试访问Java中的静态WCF服务时,在DefaultHttpClient附近获取异常。这是我的代码 - Getting exception near DefaultHttpClient when i am trying to access restful wcf service in java.Here is my code 我是否可以在同一应用程序中运行RESTful服务和angularJS以避免交叉浏览器 - Am I able to run a RESTful Service and angularJS in the same app to avoid CROSS Browser 为什么我的两个不同的angularjs服务解析为相同的静态Java EE Web服务? - Why are my two different angularjs services resolving to the same restful Java EE web service? 在restful Web服务中访问HttpServletRequest对象 - Getting Access to HttpServletRequest object in restful web service 部署与Spring RESTful Web服务通信并返回JSON数据的AngularJS Project时出错 - Error While Deploying AngularJS Project which communicates with a Spring RESTful web service returning JSON data 使用Spring Restful Web Service时遇到org.springframework.web.servlet.PageNotFound.noHandlerFound错误 - Getting org.springframework.web.servlet.PageNotFound.noHandlerFound error, while using Spring Restful Web Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM