ajax.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // ajax操作
  2. /*
  3. 创建XMLHttpRequest。
  4. */
  5. function newXHR() {
  6. var xmlHttp;
  7. try {
  8. xmlHttp=new XMLHttpRequest(); // Firefox,Google
  9. } catch (e) {
  10. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); // IE
  11. xmlHttp.abort();
  12. }
  13. if (xmlHttp) return xmlHttp;
  14. throw "Unsupport ajax.";
  15. }
  16. /*
  17. Ajax HEAD请求
  18. */
  19. function ajaxhead(url,callback) {
  20. // 创建XMLHttpRequest
  21. var xmlHttp=newXHR();
  22. xmlHttp.open("HEAD",url,false);
  23. xmlHttp.send(null);
  24. callback(xmlHttp);
  25. }
  26. /*
  27. Ajax GET请求
  28. */
  29. function ajaxget(url,callback) {
  30. // 创建XMLHttpRequest
  31. var xmlHttp=newXHR();
  32. xmlHttp.open("GET",url);
  33. xmlHttp.onreadystatechange=function() { callback(xmlHttp); };
  34. xmlHttp.send(null);
  35. }
  36. /*
  37. Ajax POST请求
  38. */
  39. function ajaxpost(url,callback,parameters) {
  40. // 创建XMLHttpRequest
  41. var xmlHttp=newXHR();
  42. xmlHttp.open("POST",url);
  43. xmlHttp.onreadystatechange=function() { callback(xmlHttp); };
  44. xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  45. xmlHttp.send(parameters);
  46. }
  47. /*
  48. Ajax GET同步请求
  49. */
  50. function ajaxgetsync(url) {
  51. // 创建XMLHttpRequest
  52. var isFirefox=navigator.userAgent.indexOf("Firefox")>0;
  53. var xmlHttp=newXHR();
  54. var result;
  55. xmlHttp.open("GET",url, false);
  56. if (!isFirefox) {
  57. xmlHttp.onreadystatechange=function() {
  58. if (xmlhttp.readyState==4)
  59. {
  60. result = xmlhttp.responseXML;
  61. }
  62. }
  63. }
  64. xmlHttp.send(null);
  65. if (isFirefox) {
  66. result = xmlhttp.responseXML;
  67. }
  68. alert("result:" + result);
  69. return result;
  70. }