0

我想从 servlet 获取对象。

我尝试下面的代码,但得到 "[object Object]" 。我想要“描述”值。

我在浏览器中运行http://www.host.com/f/ServletToJSP1/ *

o/p {“描述”:“Nutanix 提供颠覆性的数据中心基础架构解决方案,这些解决方案具有超高效率、可大规模扩展且非常简单。”}

在控制台日志中:Uncaught ReferenceError: google is not defined

我怎样才能做到这一点 ?

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

小服务程序代码

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);
4

3 回答 3

0

访问description响应对象的属性。

          $.ajax({
                url : 'ServletToJSP1', 
                type : 'GET',
                dataType : 'json', 
                success : function(response) {
                    //response = $.parseJSON(response);
                    alert(response.Description);
                },
                error : function(error) {
                    //error handling....
                    alert(error);
                }
            });
于 2013-09-10T08:32:45.360 回答
0

尝试

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

并在您的 servlet 代码中尝试添加out.flush();

于 2013-09-10T08:37:36.573 回答
0

你在用 Chrome 开发吗?您可以使用 CTRL+SHIFT+I 打开开发工具,然后打开控制台。

代替 alert(response) 尝试使用 console.log(response) 来更深入地了解变量。

于 2013-09-10T08:35:10.790 回答