function ajax(method,url,data,fn){ // 1、创建对象 var xhr=null; try{ xhr=new HttpRequest(); }catch(e){ xhr=new ActiveX ("Microsoft. HTTP"); } // 2、open方法 if(method=="get"&&data){ url=url+"?"+data; } xhr.open(method,url,true); // 3、send方法 if(method=="get"){ xhr.send() }else{ // post请求时执行 // 声明发送的数据类型 xhr.setRequestHeader('content-type','application/x-www-form-urlencoded'); xhr.send(data); } // 4、接收数据 xhr. =function(){ if(xhr.readyState==4){ if (xhr.status==200) { // 数据接收成功后执行传来的函数 fn(xhr.responseText) }else{ alert("错误"+xhr.status) } } } } 注:function ajax(method,url,data,fn){} method----方法 url---路径 data---数据,不用传数据时,函数传该参数"" fn---数据接收成功后执行传来的函数
继续阅读与本文标签相同的文章
上一篇 :
用户空间和内核空间
下一篇 :
移动端rem实现自适应布局实例讲解
-
企业为什么要上云?企业如何上云?
2026-05-19栏目: 教程
-
工作流在Kubernetes集群中的实践
2026-05-19栏目: 教程
-
如何从5万设备中找出频繁掉线设备,长期不在线的设备?
2026-05-19栏目: 教程
-
零售数据观(一):如何花30分钟成为一个标签设计“达人”
2026-05-19栏目: 教程
-
开放下载!《长安十二时辰》爆款背后的优酷技术秘籍首次公开
2026-05-19栏目: 教程
