Ajax使用JSON数据格式
<span style="font-size:18px;">{"person": { "name":"Andy Budd", "website":"http://andybudd.com/", "email":"[email protected]" } }</span>
<%@ page language="java" pageEncoding="UTF-8"%> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>People at Clearleft</title> <style type="text/css"> @import url("clearleft.css"); </style> <script type="text/javascript"> window.onload=function(){ var aNodes=document.getElementsByTagName("a"); for(var i = 0;i < aNodes.length; i++){ aNodes[i].onclick=function(){ var request=new XMLHttpRequest(); var url=this.href; var method="GET"; request.open(method,url); request.send(null); request.onreadystatechange=function(){ if(request.readyState==4){ if(request.status==200||request==304){ var result=request.responseText; //json被存储在responseText属性中 var object=eval("("+result+")"); //读取responseText中的json数据 var name= object.person.name; //读取json对象中存储的数据 var website= object.person.website; var email= object.person.email; var aNode=document.createElement("a"); aNode.appendChild(document.createTextNode(name+":"+"email"+":"+website)); aNode.href="mailTo"+"email"+",website"; var h2Node=document.createElement("h2"); h2Node.appendChild(aNode); var dtails=document.getElementById("details"); details.innerHTML=""; //防止重复的添加字符串 dtails.appendChild(h2Node); } } } return false; } } }; </script> </head> <body> <h1> People </h1> <ul> <li> <a href="files/andy.js">Andy</a> </li> <li> <a href="files/richard.js">Richard</a> </li> <li> <a href="files/jeremy.js">Jeremy</a> </li> </ul> <div id="details"></div> </body> </html>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。