从HttpServletResponse获取json对象

时间:2013-09-21 13:20:10

标签: javascript ajax json jsp

我通过ajax调用发送一个json对象 当我尝试打印json的值时,我得到了空值。

我做错了什么?

的Servlet

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {


   response.setContentType("application/json");
   response.setCharacterEncoding("UTF-8");
   response.getWriter().write("[{\"PARENT\":\"2\",\"VAL\":\"X7280\"},{\"PARENT\":\"2\",\"VAL\":\"X8338\"}]");
}

的javascript:

    function handleIt()      {              
        var url = "myservlet";
        var parameters = "method=method";
        http.open("POST", url, true);        

        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", parameters.length);
        http.setRequestHeader("Connection", "close");

        http.onreadystatechange = function() {
            if(http.readyState == 4) {                      
            var ddl3 = http.responseText;                           

            for(var key in ddl3){                   
                    var id = ddl3[key].PARENT;
                    var lbl = ddl3[key].VAL;

                    alert (lbl);

         }
      }
  }
    http.send(parameters);
} 

0 个答案:

没有答案