不啰嗦了,咱们直接来精髓!!!没有对其代码的步骤进行详细的解释
/* ?*使用ajax获取数据时,可以使用此js * url: 请求地址 * fnSucc:获取响应数据函数 * fnFaild:显示失败信息请求函数 * str: 直接响应的字符串数据 *//*//响应数据使用模板ajaxRquest("http://localhost:8080/ajax/user.xml",function (str) { ???????var div1=document.getElementById("div1"); ???????div1.innerHTML=str; ???},function () { ???????alert("失败了!"); ???}) */function ajaxRquest(url,fnSucc,fnFaild) { ???????var oajax=null; ???????//创建ajax对象,初始化开始 ???????if (window.XMLHttpRequest){ ???????????oajax=new XMLHttpRequest(); ???????}else if(window.ActiveXObject){ ???????????oajax=new ActiveXObject("Microsoft.XMLHTTP"); ???????} ???????????????//2.创建连接 ???????oajax.open("get",url,true); ???????//3.发送请求 ???????oajax.send(); ???????//4.接收返回 ???????oajax.onreadystatechange=function () { ???????????if(oajax.readyState==4){ ???????????????if (oajax.status==200){ ???????????????????// alert(oajax.responseText); ???????????????????fnSucc(oajax.responseText); ???????????????}else { ???????????????????// alert("出错了"); ???????????????????if(fnFaild){ ???????????????????????fnFaild(); ???????????????????} ???????????????} ???????????} ???????}}
/*
使用ajax如何拿取数据?简单封装
原文地址:https://www.cnblogs.com/Treying/p/9241503.html