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);
?>
还没有评论,来说两句吧...