昨天刚考了科二,一直没有时间学习,而且还没有过。。。
Ajax服务器发送请求的基本框架:
首先是html部分:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="button" value="发送请求报文" />
</body>
</html>
<script type="text/javascript">
//绑定点击事件
document.querySelector('input').onclick=function(){
//创建对象 异步对象
var xhr=new XMLHttpRequest();
//请求行
//get请求 数据是拼接在url中
//xxx.php?key=value&key2=value2
xhr.open('get','xxx.php?name=javk&skill=painting');
//注册回调函数
//请求响应回来之后触发
xhr.onload=function(){
console.log('请求响应回来啦');
console.log(xhr.responseText);
alert(xhr.responseText);
}
//请求头
//参数1 键名
//参数2 值
//目前没有什么作用
//get请求头 可以省略 设置请求头的操作
//xhr.setRequestHeader('heima','goodgoodstudy day day up');
//请求主体
xhr.send(null);
}
</script>
下面是php部分(文件名xxx.php):
<?php
echo'你来了呀';
//get提交过来的数据 使用 $-GET来获取
print_r($_GET);
//延迟3秒
sleep(3);
?>