You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.5 KiB

  1. /*---------------------------- Ajax 请求对象 ----------------------------*/
  2. /**************************************************
  3. -- 属性
  4. url: 请求地址
  5. method: 请求方法GET 或者 POST默认为 GET
  6. flag: 异步或者同步请求true 或者 false默认为 true
  7. info: 请求参数默认为 null只有当请求方法为 POST 才被使用
  8. //isEval: 是否调用 eval() 函数(true 或者 false),默认为为 false;
  9. //callback: 是否设置回调函数(true或者false),默认为 true;
  10. result: Ajax 请求的响应结果
  11. -- 方法
  12. executeRequest: 执行 Ajax 请求的方法
  13. onReady: 绑定函数到 req.onreadystatechange 事件
  14. dealResult: 只提供了函数名称用来设置用户自己的函数在用户自己的函数中用来处理 Ajax 请求的返回结果
  15. **************************************************/
  16. function Ajax(url, method, flag, info) {
  17. this.url = url;
  18. this.method = method || "GET";
  19. this.flag = (flag == false) ? false : (flag || true);
  20. this.info = info || null;
  21. //this.isEval = false;
  22. //this.callback = true;
  23. this.result = null;
  24. var req = null; // req: XMLHttpRequest 对象;
  25. // 实例化 req 对象
  26. (function() {
  27. if (window.XMLHttpRequest) {
  28. req = new XMLHttpRequest();
  29. if (req.overrideMimeType) {
  30. //req.overrideMimeType("text/xml");
  31. }
  32. } else if (window.ActiveXObject) {
  33. try {
  34. req = new ActiveXObject("Msxml2.XMLHTTP");
  35. } catch (e) {
  36. try {
  37. req = new ActiveXObject("Microsoft.XMLHTTP");
  38. } catch (e) {
  39. alert("该浏览器不支持 Ajax !");
  40. req = null;
  41. }
  42. }
  43. /* Msxml2.XMLHTTP.5.0, 4.0, 3.0 2.0 1.0
  44. for (var i = 5; i; i--) {
  45. try {
  46. alert(i);
  47. if (i != 2) {
  48. req = new ActiveXObject("Msxml2.XMLHTTP." + i + ".0");
  49. }
  50. } catch (e) {
  51. alert(e);
  52. continue;
  53. }
  54. }*/
  55. }
  56. }());
  57. this.executeRequest = function () {
  58. if (!req || !this.url) {
  59. alert("req 为空或者 url 为空!");
  60. return;
  61. }
  62. if (this.url.indexOf("_id_=") != -1) {
  63. // 不改变
  64. } else {
  65. // 保证每次请求的 URL 都不一样,防止缓存
  66. var rand = "_id_=" + Math.random();
  67. if (this.url.indexOf("?") != -1) {
  68. this.url += "&" + rand;
  69. } else {
  70. this.url += "?" + rand;
  71. }
  72. }
  73. /*// 判断是否设置回调函数
  74. if (this.callback) {
  75. this.onReady(req);
  76. }*/
  77. // 设置回调函数
  78. this.onReady(req, this);
  79. // 初始化 req 对象
  80. req.open(this.method, this.url, this.flag);
  81. // 判断,是否为 POST 请求
  82. if ((this.method.toUpperCase() == "POST") && this.info) {
  83. req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  84. }
  85. // 向服务器端发送请求
  86. req.send(this.info);
  87. /*// 判断是否执行 eval() 函数
  88. if (this.isEval) {
  89. eval(req.responseText);
  90. }*/
  91. };
  92. this.onReady = function (req, ajax) {
  93. req.onreadystatechange = function () {
  94. if (req.readyState == 4) {
  95. var result = req.responseText;
  96. if (req.status == 200) {
  97. ajax.result = result;
  98. if (ajax.dealResult) {
  99. ajax.dealResult(result);
  100. } else {
  101. var message = "请为 Ajax对象的 dealResult 设置回调函数!";
  102. message += "\r\n例如:Ajax对象.dealResult=function(result) { Your Code }";
  103. message += "\r\n其中,result 参数为服务器端返回的响应数据。";
  104. //alert(message);
  105. }
  106. } else {
  107. //alert("响应错误!");
  108. //alert(result);
  109. }
  110. }
  111. };
  112. };
  113. this.dealResult; // = function(result) { };
  114. }