js中使用ajax 并对返回的结果进行解析

来源:互联网 发布:python 真假 编辑:程序博客网 时间:2024/05/23 10:43

js代码:

  

function friend(id){$.ajax({type:"post",url: "friendContorller.do?ajax",  data:{'id':id},typedata:"json",success: function (data) {//这里将返回的data转化成json对象var  json = JSON.parse(data); get('tab1').innerHTML="";//这里是获取json对象中的birthday的属性var birthday=json[0].birthday==""?"unknow":json[0].birthday;var height=json[0].height==""?"unknow":json[0].height;var relationship=json[0].relationship=""?"unkonw":json[0].relationship;get('tab1').innerHTML="<h2>name: "+json[0].name+"</h2>"+      "<h2>birthday: "+birthday+"<h2>"+    "<h2>height: "+height+"<h2>"+    "<h3>relationship: "+relationship+"<h3>";get('image').innerHTML="";get('image').style.backgroundImage="url(images/1.jpg)";}});}



 

Java代码:这里我使用的是springmvc的注解形式

@Autowiredprivate GetInformationImpl getInformation;@RequestMapping(params="ajax")@ResponseBodypublic void ajax(String id, HttpServletResponse response){Friend friend=getInformation.getFriendInformationDetail(id);JSONObject json=JSONObject.fromObject(friend);try {response.getWriter().write("["+json.toString()+"]");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


 

0 0
原创粉丝点击