I went through a lot of StackOverflow answers and googled a lot but still could not find why my post request is not working.
This is my jsp:
You are using a POST
request and your data is sent in the request body - not as parameter. You need to read the content using request.getReader()
.
// example using the javax.json.stream package
JsonParser parser = Json.createParserFactory().createParser(request.getReader());
Your request does not contain a URL parameter named "data", therefore request.getParameter("data") returns null and you get the NullPointerException.
You try to send a Javascript object via URL parameters which does not go well with non-shallow objects.
I would recommend to send the data as request payload:
JsonObject obj = (JsonObject) parser.parse(request.getReader());
On the client you need to make sure that your data is sent as proper JSON:
$http({
method : 'POST',
url : 'login',
contentType: 'application/json',
data : JSON.stringify($scope.user),
})...
i think you should be sending data as
$http({
method : 'POST',
url : 'login',
data : {data: $scope.user},
headers: {
'Content-Type': 'application/json'
}
}).success(function(data) {
console.log(data);
});
pay attention to data : {data: $scope.user}