后端用Json传值给前端及前端接收的案例

后端代码:

StringBuffer stringBuffer = new StringBuffer("test123");

JSONObject jo = new JSONObject();
jo.put("content", stringBuffer.toString());
try {
	response.setContentType("text/json");
	response.setCharacterEncoding("utf-8");
	response.getWriter().print(jo);
} catch (IOException e) {
	e.printStackTrace();
}

前端ajax接收:

$.ajax({
		type:"post", 
   		async:false,
   		url:"DelJITASNTxt.do?id="+str,
   		dataType:"json",  
   		success: function (data) {
   			var content = data.content;
      		}
	}); 

你可能感兴趣的:(Java,java,ajax,javascript)