Ajax ajax原生-xhr对象处理get请求

前台:

<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<script src="jquery-3.4.1.js"></script>
</head>
<body>
	
	<button>发送请求</button>
	
	<script>
		var btn=document.querySelector('button');
		btn.onclick=function()
		{
			//创建xhr对象
			var xhr=new XMLHttpRequest();
			//监听请求是否成功
			xhr.onreadystatechange=function()
			{
				//通过readyState属性的值,判断当前请求状态
				if(xhr.readyState==4)
				{
					console.log("已接受");
					//通过status属性来判断前台接收状态
					if(xhr.status==200)
					{
						console.log(xhr.statusText);
						//此时表明真正接收到了数据
						console.log(xhr.responseText);
						console.log(JSON.parse(xhr.responseText));
					}
				}

			};

			//发送ajax
			//设置发送
			//无参get
			xhr.open('get','3.php',true);
			//有参get
			xhr.open('get','3.php?uame='+uname+'&upwd='+upwd);
			//开始发送
			xhr.send(null);
			
		}

	</script>

</body>

</html>

后台:

<?php
	$success=array('mes'=>'ok');
	echo json_encode($success);



?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值