var xhr = new XMLHttpRequest;
xhr.open('get', '03.php?name=itcast1111'); //get写法 post写法把 name放到xhr.send里面
//请求行传入第三个参数是true 表示异步 false表示同步 默认异步true
xhr.setRequestHeader('Content-Type', 'text/html'); //get写法可以不写 post写法xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(null); //get请求没有请求主体
//响应主体
xhr.onreadystatechange = function () {
if(xhr.readyState == 4 && xhr.status == 200) {
var result = xhr.responseText;
document.querySelector('.result').innerHTML = result;
}
}
var xhr = new XMLHttpRequest;
xhr.open('post', '03.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('name=itcast&age=10');
xhr.onreadystatechange = function () {
if(xhr.readyState == 4 && xhr.status == 200) {
var result = xhr.responseText;
document.querySelector('.result').innerHTML = result;
}
}