zoukankan      html  css  js  c++  java
  • 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;	
    ?>
    

      

  • 相关阅读:
    (14) go 结构体
    (13) go map
    (12) go make初始化
    (11)go 数组和切片
    (10) go 错误
    (9) go 时间日期
    (8)go 字符串
    (7) go 函数
    (6) go 流程控制
    (5) go 格式化输入输出 类型转换
  • 原文地址:https://www.cnblogs.com/handsomehan/p/5865270.html
Copyright © 2011-2022 走看看