将json对象返回到jquery并在UI中显示

时间:2013-02-19 17:31:04

标签: jquery ajax jsp

我能够向我的action类提交Ajax调用,并且能够使用JQuery返回“Hello”+名称并在JSP中显示。现在我想传递一个有名字,年龄和城市的对象。如果我将此对象作为JSON对象传递,如何在HTML输入字段(文本字段)中检索和显示它,如名称,年龄和城市?

function doAjaxPost() {    
 // get the form values  
  var name = $('#name').val();   

  $.ajax({  
     type: "POST",
      url: "/eappfw/AjaxSubmit.do",  
       data: "name=" + name,  
         success: function(response){  
      // we have the response as Hello with entered name 
      $('#info').html(response);  
    },  
    error: function(e){  
      alert('Error: ' + e);  
    }  
  });  
}    



//In action class

AjaxForm ajaxForm = (AjaxForm)form; 
    response.setContentType("text/text;charset=utf-8");
      response.setHeader("cache-control", "no-cache");
      PrintWriter out = response.getWriter();
      out.println("Hello " + ajaxForm.getName());
      out.flush();
      return null;  

0 个答案:

没有答案