0

I send a json from servlet like that

User profileUser = userService.get(id);
request.setAttribute("profileUser", profileUser);
JSONObject json = new JSONObject();
try {
    json.put("profileUser", profileUser);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
response.setContentType("application/json");
response.getWriter().write(json.toString());

And in javascript in ajax call

$.ajax({
    cache : false,
    type : "GET",
    url : "UserServlet?id=" + userId,
    success : function(result) {
        alert(result.profileUser);
    }
});

The result of alert is giving me Entity.user but if I want to call result.profileUser.uuid or property of that user returns me undefined . Can anyone please help me?

2
  • What's the actual HTTP response? Commented Oct 10, 2016 at 15:42
  • @SLaks 302 moved temporary Commented Oct 10, 2016 at 15:45

2 Answers 2

1

If you're sending json to your Ajax client, then you need to specify that you're about to recieve json type. Like this:

$.ajax({
    cache : false,
    type : "GET",
    dataType : "json",
    url : "UserServlet?id=" + userId,
    success : function(result) {
        alert(result.profileUser);
    }
});

That will cause your success method to evaluate the result parameter as json object.

Sign up to request clarification or add additional context in comments.

Comments

0

Change your Ajax to following:

$.ajax({
    cache : false,
    type : "GET",
    url : "UserServlet?id=" + userId,
    success : function(profileUser) {
        alert(profileUser.uuid);
    }
});

Comments

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.