ajax数据请求5(php格式)

ajax数据请求5(php格式):

<!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格式:

<?php
	$str = '{"name":"老王","sex":"男","age":"22","score":"58"}';
	echo $str;	
?>

  

posted @ 2016-09-12 16:31  HandsomeHan  阅读(762)  评论(0编辑  收藏  举报