servlet与ajax之间的数据传递

@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name=req.getParameter("name");
		Person person = new Person(name,23);
		JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setJsonPropertyFilter(new net.sf.json.util.PropertyFilter(){
          public boolean apply(Object source, String name, Object value) {
              if(name.equals("person")){
                  return true;
              }else{
              return false;
              }
          }});
      
     JSONArray json=JSONArray.fromObject(person,jsonConfig);
         try {
      	resp.setContentType("text/html;charset=gb2312");
          PrintWriter out=resp.getWriter();
          out.println(json.toString());
          out.flush();
          out.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
	}





      function initXtwhYhwh()
  	  { 
      	   	var id = $("#bmbz").val(); 
      	    $.ajax({
  		    type: "POST",
  		    dataType:"json",
  		    url: "<%=SystemResource.DOMAIN%>/XtwhYhwhServlet",
  		     data: "id="+id,
  		    success: function(xtwhYhwhs){
  		   	  $("#xtwhyh").empty();
  			  var i =$("#xtwhyh").val();
  			  $(xtwhYhwhs).each(function(i){
  					   $("#xtwhyh").append("<option name='xtwhyh'  value=\""+xtwhYhwhs[i].userid+"\">"+xtwhYhwhs[i].username+"</option>");
  			  });
  		   }
  		});
  		  
  	  }



导包 json-lib-2.4-jdk15.jar    一开始报错,问题不详,servlet重新写了一遍没有报错

JSONObject 的使用

项目中导入了 json-lib 包,但是在运行如下代码的时候还是出现了如下错误,究其原因是因为缺包,缺了如下5个包:

commons-beanutils-1.8.3

commons-lang-2.6 (注:导入最新的 3.1 版本会继续报如下错误)

commons-collections-3.2.1
commons-logging-1.1.1

ezmorph-1.0.6

Json Lib 下载:json-lib-2.4

猜你喜欢

转载自everlxq.iteye.com/blog/1911256
今日推荐