ITeye博客

ajax封装类

diaoweili · 2010-11-10 18:57 · Ajax学习开发应用 · Web前端 · Ajax · XML · Microsoft · Markdown

  1. function ajax(method,synchronous){
  2. this._httpReq = false;  /*ajax初始化对象*/
  3. this.method = method;  /*get|post*/
  4. this.syn = synchronous;   /*是否采用异步请求,默认true*/
  5. this.url = "";   /*提交异步请求的url地址*/
  6. this.resType = "";  /*异步请求返回数据类型text|xml*/
  7. this.callback = "";  /*异步请求完成后的回滚函数*/
  8. this.loading = "";  /*load函数*/
  9. this.content = null; /*Ajax中send方法的参数*/
  10. this.readystate = -1; /*ajax的请求状态*/
  11. this.state = -1;  /*http请求响应代码*/
  12. /************ get/set方法开始 ***************/
  13. //设置提交异步请求的url地址
  14. this.setUrl = function (url){
  15. this.url = url;
  16. }
  17. //设置异步请求返回数据类型text|xml
  18. this.setResType = function (restype){
  19. this.resType = restype;
  20. }
  21. //设置回滚函数
  22. this.setCallback = function (func){
  23. this.callback = func;
  24. }
  25. //设置load函数
  26. this.setLoading = function (loadFunc){
  27. this.loading = loadFunc;
  28. }
  29. //设置send自带的参数值,默认null
  30. this.setContent = function (contents){
  31. this.content = contents;
  32. }
  33. /*********get/set方法结束*******/
  34. /*********状态显示方法*********/
  35. //调用window.alert方法
  36. this.alert = function (msg){
  37. window.alert(msg);
  38. }
  39. //调用window.status的方法
  40. this.status = function (msg){
  41. window.status = msg;
  42. }
  43. /*********状态显示方法结束*********/
  44. /*************执行方法开始*****************/
  45. //创建HttpXMLRequest
  46. this.createXMLRequest = function(){
  47. if(window.XMLHttpRequest){
  48. this._httpReq = new XMLHttpRequest();
  49. if(this._httpReq.overrideMimeType){
  50. this._httpReq.overrideMimeType("text/xml");
  51. }
  52. }else if(window.ActiveXObject){
  53. try{
  54. this._httpReq = new ActiveXObject("Msxml2.XMLHTTP");
  55. }catch(e){
  56. try{
  57. this._httpReq = new ActiveXObject("Microsoft.XMLHTTP");
  58. }catch(e){}
  59. }
  60. }
  61. }
  62. //初始化ajax对象
  63. this.init = function(){
  64. this.createXMLRequest();
  65. }
  66. //发送一个http请求
  67. this.send = function (){
  68. if(this.resType.toLowerCase()=="post"){
  69. _httpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  70. }
  71. this._httpReq.open(this.method,this.url,this.syn);
  72. this._httpReq.send(this.content);
  73. }
  74. //取消一个http请求
  75. this.abort = function (){
  76. this._httpReq.abort();
  77. }
  78. this.callbackState = function(){
  79. switch(this._httpReq.readyState){
  80. case 0:
  81. this.readystate = 0;
  82. break;
  83. case 1:
  84. this.readystate = 1;
  85. break;
  86. case 2:
  87. this.readystate = 2;
  88. break;
  89. case 3:
  90. this.readystate = 3;
  91. break;
  92. case 4:
  93. this.readystate = 4;
  94. switch(this._httpReq.status){
  95. case 200:
  96. eval(this.callback);
  97. break;
  98. case 202:
  99. this.status("请求处理中,还没处理完毕!");
  100. break;
  101. case 400:
  102. this.status("错误的请求!");
  103. break;
  104. case 404:
  105. this.status("请求资源未找到!");
  106. break;
  107. case 500:
  108. this.status("内部服务器错误,请联系管理员!");
  109. break;
  110. default:
  111. this.status("返回数据失败,"+this._httpReq.status);
  112. break;
  113. }
  114. break;
  115. default:
  116. this.readystate = 0;
  117. break;
  118. }
  119. }
  120. this.onReadyStateChange = function (){
  121. var owner = this;
  122. this._httpReq.onreadystatechange = function(){
  123. owner.callbackState.call(owner);
  124. }
  125. }
  126. /*************执行方法结束*****************/
  127. }

评论(2)

  1. #2 楼 · jmwjy · 2012-07-05 14:24

    还有,能否指点下这个问题

    看了你的文章,我才我发现我好象也是要写一个封装(封装对我来说,太高深了),但是总有问题。

    http://bbs.51js.com/thread-90328-1-1.html

  2. #1 楼 · jmwjy · 2012-07-05 14:20

    博主,能否指点下,这个怎么调用…… 我正在学ajax,希望您指点下

评论功能维护中,当前仅展示历史评论。