-1

I have angular code

app.controller('add', function ($scope, $http) {
    $scope.add = function() {
        $scope.msg = "no connect";

        var data = {name:'soso',description:'buba',method:'POST'};

        $http.post(host + "/add-component",data)
            .then(function (response) {
                $scope.msg = response.data;
            });

    }
});

in my servlet, I want to catch it

resp.setContentType("application/json; charset = utf8");
String name = req.getParameter("name");
String description = req.getParameter("description");

but my name and description both = null;

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103

1 Answers1

0

You are sending data as POST request body, not request parameters. This is documented in the angular docs for post method:

url string
Relative or absolute URL specifying the destination of the request

data *
Request content

Take a look at this answer to see how to read request body.

These days it's easier to use Spring Boot or other frameworks to handle your server side endpoints. There is nothing wrong with using Servlets but you will have to write more code yourself.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Thank you so much! It realy help for me. Map params = new HashMap<>(); String[] body = req.getReader().lines().collect(Collectors.joining()).split("\""); for (int i = 1; i < body.length; i += 4) { params.put(body[i], body[i + 2]); } name = params.get("name"); – Mihail Kolomiets Aug 25 '18 at 11:43