原生ajax实现方式
2024-08-31 15:55:01
http://www.cnblogs.com/rubylouvre/archive/2013/01/08/2851051.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> Ajax = function(){ function request(url,opt){ function fn(){} var async = opt.async !== false, method = opt.method || 'GET', data = opt.data || null, success = opt.success || fn, failure = opt.failure || fn; method = method.toUpperCase(); if(method == 'GET' && data){ url += (url.indexOf('?') == -1 ? '?' : '&') + data; data = null; } var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); xhr.onreadystatechange = function(){ _onStateChange(xhr,success,failure); }; xhr.open(method,url,async); if(method == 'POST'){ xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;'); } xhr.send(data); return xhr; } function _onStateChange(xhr,success,failure){ if(xhr.readyState == 4){ var s = xhr.status; if(s>= 200 && s < 300){ success(xhr); }else{ failure(xhr); } }else{} } return {request:request}; }(); console.log(Ajax.request("http://baidu.com",{method:"POST"})); </script> </body> </html>
最新文章
- Zabbix自定义监控8080端口的连接数
- Java基础——继承、接口
- <;s:property value=";";/>; 怎么截取返回值的固定长度的字符串
- gpio高阻态
- idea 小技巧
- mysql在Linux下
- @weakify, @strongify ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify
- WeakReference(弱引用)
- rubycas-client单点登录
- MVC 中的 ViewModel
- 采访ServiceStack的项目领导Demis Bellot——第2部分(转)
- No valid host was found. There are not enough hosts available
- HDU 4927 大数运算
- [收藏转贴]struct探索&#183;extern ";C";含义探索 &#183;C++与C的混合编程&#183;C 语言高效编程的几招
- NoSql中的B-tree、B+tree和LSM-tree
- MFC中SDI程序创建流程的回顾
- BOM简单知识
- 汲取营养的blog专栏
- Python之函数第三篇
- RabbitMQ(3) Spring boot集成RabbitMQ
热门文章
- CAD二次开发(02)-添加对象到模型空间
- Qt之水平/垂直布局(QBoxLayout、QHBoxLayout、QVBoxLayout)
- dll签名两种方法
- 解决The hierarchy of the type is inconsistent错误
- 部署zookeeper实践
- MyEclipse常见错误汇总,中英注释版(长期更新)
- bzoj1005: [HNOI2008]明明的烦恼(prufer+高精度)
- Fast Flux技术——本质就是跳板,控制多个机器,同一域名指向极多的IP(TTL修改为0),以逃避追踪
- hdoj--2282--Chocolate(最小费用)
- Lamp安装 php-v5.6【ZendGuardLoader】的问题