1

I am learning Angular.js. I tried to search many existing questions but could not find one matching question or answer.

I am sending a GET request to a java servlet and expecting an object from it. Here is the angular $http GET request

        $http({
            method:'GET',
            url : '/JSONWebApp/json/?fqn=' + $scope.fqn
        }).
            success(function(data, status, header, config){
                console.log('Success');
                console.log("data - "+data);
                console.log("status - " + status);
                console.log("header - " + header);
                console.log("config - " + config);
            }).
            error(function(data, status, header, config){
                console.log('Error');
            });

On java servlet, here is what I write in doGet method

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       System.out.println("Incoming Request URI : " + request.getRequestURI());

   **User user = new User();
         request.setAttribute("user", user);
   RequestDispatcher view = request.getRequestDispatcher("/html/index.html");
   view.forward(request, response);**
}

When the response is returned from this servlet, it goes to success of $http service in angular side. From where I will get the user object which I sent from server.

When I add the console.log to print the data, it prints the /html/index.html contents which is where I am forwarding.

Based on my research, my angular code is correct but I am not setting things correctly in java servlet.(Does this help - How to access json return values in angular.js action success callback)

Thanks in advance.

2 Answers 2

4
  1. Add the following line to the $http object: headers: {'Content-Type': 'application/x-www-form-urlencoded'}

  2. Use POST method instead of GET method: 'POST'

So in the end, we came up with something like this:

myData = {"name": "zezinho", "pass": "123xxx"};

$http({
  method: 'POST',
  url: 'Login',
  data: myData,
  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data) {
  alert(data);
}).error(function () {
  alert("login error");
});
Sign up to request clarification or add additional context in comments.

Comments

0

You need to serialize your User object into a JSON string and put that in the response body. A library like Jackson can help but before you add that in and confusing yourself more just serialize it into a JSON string yourself. Don't forget to set the Content-Type header.

3 Comments

JSON is fine but how do we write it to request body in servlet?
I have not worked with the servlet API directly for a long time but it will be something similar to response.getWriter().print("{\"name\": \"RajaM\"}"). Also, do you need to use Servlets? If it's not a business requirement there are easier ways to create a JSON API.
That's actually correct. I was able to send JSON to angular. Just one correction: In addition to writing json to response.writer we do not have to dispatch to any of the view otherwise will will again be sending the html page as data. We just have to flush out the writer after setting the json to response. Thanks Loc.

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.