1

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

3
  • why not simply returning the response variable ? Commented Dec 30, 2016 at 7:24
  • problem is not of response, I don't get the values in variables defined in parameter. Commented Dec 30, 2016 at 7:26
  • Ok. It was not very clear. I did a response. When you post code, use the button to indent code otherwise the display may be broken. Commented Dec 30, 2016 at 7:45

2 Answers 2

2

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) {
   ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

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)

1 Comment

The problem is in sending the data not in the recieving.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.