0

I want to fetch object from servlet.

I try below code but I get "[object Object]" . I want "Description" value.

I got out in browser when I run http://www.host.com/f/ServletToJSP1/*

o/p {"Description":"Nutanix provides disruptive datacenter infrastructure solutions that are hyper-efficient, massively scalable and elegantly simple."}

in console log :Uncaught ReferenceError: google is not defined

How can I do that ?

jsp code
                  $.ajax({
                        url : 'ServletToJSP1', 
                        type : 'GET',
                        dataType : 'json', 
                        success : function(response) {
                            //response = $.parseJSON(response);
                            alert(response);
                        },
                        error : function(error) {
                            //error handling....
                            alert(error);
                        }
                    });

servlet code

JSONObject objTw = new JSONObject();
      objTw.put("Description", "Nutanix provides disruptive datacenter infrastructure solutions that are hyper-efficient, massively scalable and elegantly simple.");
PrintWriter out = response.getWriter();
response.setContentType("application/json"); 
response.setCharacterEncoding("utf-8"); 
  out.println(objTw);
Nick
  • 341
  • 3
  • 7
  • 22

3 Answers3

0

Access the description property on the response object.

          $.ajax({
                url : 'ServletToJSP1', 
                type : 'GET',
                dataType : 'json', 
                success : function(response) {
                    //response = $.parseJSON(response);
                    alert(response.Description);
                },
                error : function(error) {
                    //error handling....
                    alert(error);
                }
            });
Kevin Bowersox
  • 88,138
  • 17
  • 142
  • 176
0

Are you developing in Chrome? You can open the dev tools with CTRL+SHIFT+I then open the Console.

Instead of alert(respone) try console.log(response) to have a deeper look into the variable.

Tammo
  • 1,128
  • 1
  • 8
  • 11
0

Try

success : function(response) {
                  alert(response[0].Description);
                },

and in your servlet code try to add out.flush();

gjman2
  • 862
  • 17
  • 28
  • No this not help . alert is not come you ans is more far – Nick Sep 10 '13 at 08:38
  • @Nick what that should mean ? Please take your time to spell the words completely and to describe accurately what is happening. BTW, are you saying that the alert is not reached ? But it's the first row of the "success" result... where do you see the [Object object] then ? – Andrea Ligios Sep 10 '13 at 08:40