原生Ajax总结

时间:2022-04-22
本文章向大家介绍原生Ajax总结,主要内容包括HTTP协议、Ajax定义、Ajax基本流程、jQuery中Ajax模块、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

HTTP协议

传统的请求和Ajax请求

Ajax定义

Asynchronous JavaScript and XML. Ajax异步的,JavaScript程序希望与服务器直接通信而不需要重新加载页面。

Ajax基本流程

1.创建请求对象

function requestObject(){
            if(window.XMLHttpRequest){
                return new XMLHttpRequest();
            }else if{
                return new ActiveXObject('Mxsml2.XMLHTTP');
            }else{
                throw new Error("Could not create HTTP request object.");
            }
        }

2.建立请求 var request=requestObject(); request.open("GET","data.txt",true);

3.发送请求

request.send(null);

4.处理请求(XML和JSON两种格式)

request.onreadystatechange=function(){
            if(request.readyState==4){
                console.log(request.status+":"+request.statusText);
            }
        }

jQuery中Ajax模块

参考内容:

图解HTTP

jQuery源码Ajax模块分析

使用jQuery