[TOC]
>[success] # 回调函数&获取返回的数据
>[success] ## 前端代码
~~~
Documentwindow.onload = function(){
// 绑定点击事件
document.querySelector('input').onclick = function(){
// 创建对象: 异步对象
var xhr = new XMLHttpRequest();
// 请求行
// get请求是拼接在 url中
// xxx.php?key=value&key2=value2
xhr.open('get','xxx.php?name=jack&skill=painting');
// 注册回调函数
// 请求响应回来之后触发
xhr.onload = function(){
// 接口返回数据
console.log(xhr.responseText)
}
// 请求头 setRequestHeader,Cookie就是加在请求头里面的
// 参数1 键名
// 参数2 值
// 目前这个好像没有任何作用 是否可以省略呢?
// get请求 可以省略 设置请求头的操作
// xhr.setRequestHeader('aoligei','hahahah')
// 请求主体 发送
xhr.send(null)
}
}
~~~
>[success] ## 后端接口代码
~~~
echo '你来了阿';
// get提交过来的数据 使用 $_GET来获取
print_r($_GET);
// 延迟一会
sleep(3);
?>
~~~