1.创建一个XMLHttpRequest异步对象
段落引用new XMLHttpRequest()
2.设置请求方式和请求地址
open("GET", "http://localhost:3003/article", true)
3.接着,用send发送请求
send() send("fname=Henry&lname=Ford");
4.监听状态变化
Onreadystatechange=function(){}
5.最后,接收返回的数据
responseText
<script>
function ajaxfn() {
// 创建一个XMLHttpRequest异步对象
var xmlhttp = new XMLHttpRequest();
// 设置请求方式和请求地址
xmlhttp.open("GET", "http://localhost:3003/article", true);
// send发送请求
xmlhttp.send();
// 监听状态变化
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// xmlhttp.readyState == 4表示请求已完成
// xmlhttp.status == 200表示成功
// xmlhttp.status == 404未找到页面
console.log('object');
console.log(xmlhttp.responseText);
console.log(xmlhttp.getAllResponseHeaders());
console.log(xmlhttp.getResponseHeader('content-type'));
}
}
}
ajaxfn()
</script>