Ajax基础

1. 概念:AJAX(Asynchronous JavaScript and XML)为异步的JS和XML。
2. AJAX用于数据的交换,使网页实现异步更新,可以只对网页的某个部分进行更新。
3. Ajax详细的请求步骤:
a. 创建出XMLHttpRequest对象【需要考虑浏览器的兼容性】

var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP') ;

b. 配置请求(初始化请求信息)

xhr.open('GET', 'data.json', true); 
// xhr.open('POST', 'data.json', true);

c. 发送请求

xhr.send(null); //get方 式
// xhr.send('usr=123&pass=123');//post发 送 方 式

d. 监听请求结果

xhr.onreadystatechange = function(res) {
if (xhr.readyState == 4 && xhr.status == 0) {
var result = JSON.parse(xhr.responseText);
console.log(result.data);

}

 

posted @ 2020-05-28 14:23  清风园  阅读(142)  评论(0编辑  收藏  举报