有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用ajax从servlet获取价值

我想从servlet获取对象

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

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

o/p{“Description”:“Nutanix提供破坏性的数据中心基础设施解决方案,这些解决方案具有超高效、大规模可扩展和优雅简单的特点。”

控制台日志中:未捕获引用错误:未定义google

我该怎么做

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代码

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

共 (2) 个答案

  1. # 1 楼答案

    试一试

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

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

  2. # 2 楼答案

    访问响应对象上的description属性

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