标签:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ajax请求数据3</title> </head> <body> <button id="btn">请求数据</button> <h1 id="txt"></h1> <script type="text/javascript"> var btn = document.getElementById(‘btn‘); var txt = document.getElementById(‘txt‘); btn.onclick = function() { //1.创建XMLHttpRequest对象 var xhr = null; if(window.XMLHttpRequest) {//非IE56 xhr = new XMLHttpRequest();//实例对象 }else {//IE56 xhr = new ActiveXObject(‘Microsoft.XMLHTTP‘); } //2.打开与服务器的链接 xhr.open(‘get‘,‘test3.php?_=‘ + new Date().getDate(),true);//生成不一样的url解决缓存问题 //3.发送给服务器 xhr.send(null);//get请求 //4.响应就绪 xhr.onreadystatechange = function() { if (xhr.readyState == 4) {//请求完成 if(xhr.status == 200) { var json = JSON.parse(xhr.responseText); txt.innerHTML = json.sex; }else { alert(xhr.status); } } } } </script> </body> </html>
<?php $str = ‘{"name":"老王","sex":"男","age":"22","score":"58"}‘; echo $str; ?>
标签:
原文地址:http://www.cnblogs.com/handsomehan/p/5865270.html