0

I am trying to use angularjs POST (or even GET) to a servlet with the following:

var json = { hello: "world" }

var deffered = $q.defer();
$http({
    method: "POST",
    url: url,
    headers: { "Content-Type" : "application/json" },
    request: JSON.stringify(json)
}).then(data) {
     if(data.data) {
          deferred.resolve({
               response : data
          });
     )
})
return deffered.promise;

within the servlet, simple:

String val = request.getParameter("request")

it never seems to see it I have tried:

data: JSON.stringify({ request: json })
data: { request: json }
"request" : JSON.stringify(json)

etc

if I comment out the getParameter and just return a generic value using Gson

JsonObject json = new JsonObject();
json.addProperty("This", "works");
response.getWriter().print(new Gson().toJson(json));

that comes back fine so is there something within the angular POST I am doing wrong here? I have also tried using "GET" instead but same result.

EDIT: I would like to understand POST method and the "proper" way to get the data from the json object if getParameter is wrong please~

Justin
  • 4,461
  • 22
  • 87
  • 152

1 Answers1

-1

getParameter() returns http request parameters, you should add this params by using :

params: JSON.stringify(json) 

Not with

request: JSON.stringify(json)

Take a look in params in get and params in post.

Community
  • 1
  • 1
Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114