在AJAX中,XMLHttpRequest对象的使用如下:
创建一个XMLHttpRequest对象:var xhr = new XMLHttpRequest();设置请求的方法和URL:xhr.open('GET', 'example.com/data', true); // true表示使用异步请求设置回调函数,处理请求的结果:xhr.onreadystatechange = function() {if (xhr.readyState === XMLHttpRequest.DONE) { // 请求完成if (xhr.status === 200) { // 请求成功console.log(xhr.responseText); // 处理返回的数据} else {console.log('请求失败');}}};发送请求:xhr.send();在上述代码中,xhr.onreadystatechange是一个事件处理函数,它会在请求的状态发生改变时被触发。在请求完成后(xhr.readyState等于4),通过xhr.status来判断请求是否成功。如果成功,可以通过xhr.responseText来获取返回的数据。
注意:在使用XMLHttpRequest对象时,可以通过xhr.setRequestHeader()方法来设置请求头部信息,也可以通过xhr.getAllResponseHeaders()方法来获取服务器返回的头部信息。

