AJAX和JSON的一些理解
正在学习JSON和AJAX,记录下自己的理解
AJAX
什么是AJAX,这里指异步JS和XML,异步JS是指客户端的JS可以不必停下来等待服务器的响应,相反该JS继续执行,而XML指的是JS用XML去解析从服务器得到的数据,如
xmlDoc=xmlhttp.responseXML//来自服务器的响应是 XML,而且需要作为 XML 对象进行解析,请使用 responseXML 属性当然,我们可以使用JSON来解析。
<html><head><script type="text/javascript">function loadXMLDoc(){var xmlhttp;if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } }xmlhttp.open("GET","/ajax/demo_get.asp",true);xmlhttp.send();}</script></head><body><h2>AJAX</h2><button type="button" onclick="loadXMLDoc()">请求数据</button><div id="myDiv"></div></body></html>
employees = [{ "firstName":"Bill" , "lastName":"Gates" },{ "firstName":"George" , "lastName":"Bush" },{ "firstName":"Thomas" , "lastName": "Carter" },{"first":"Frank","last":"Lampard"}
var obj = eval ("(" + txt + ")");中
var txt = '{"employees":[' +'{"firstName":"Bill","lastName":"Gates" },' +'{"firstName":"George","lastName":"Bush" },' +'{"firstName":"Thomas","lastName":"Carter" }]}';obj = JSON.parse(txt);