简体   繁体   中英

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. 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.

@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. With POST , you don't transmit query params with the url but you transmit data enclosed in the body of the request.

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.

@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)

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