ajax-object.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. (function () {
  2. var $ax = function (url, success, error) {
  3. this.url = url;
  4. this.type = "post";
  5. this.data = {};
  6. this.dataType = "json";
  7. this.async = false;
  8. this.success = success;
  9. this.error = error;
  10. this.contentType = 'application/x-www-form-urlencoded ';
  11. };
  12. $ax.prototype = {
  13. start : function () {
  14. var me = this;
  15. if (this.url.indexOf("?") == -1) {
  16. this.url = this.url + "?jstime=" + new Date().getTime();
  17. } else {
  18. this.url = this.url + "&jstime=" + new Date().getTime();
  19. }
  20. $.ajax({
  21. type: this.type,
  22. url: this.url,
  23. dataType: this.dataType,
  24. async: this.async,
  25. data: this.data,
  26. contentType : this.contentType,
  27. beforeSend: function(data) {
  28. },
  29. success: function(data) {
  30. me.success(data);
  31. },
  32. error: function(data) {
  33. me.error(data);
  34. }
  35. });
  36. },
  37. set : function (key, value) {
  38. if (typeof key == "object") {
  39. for (var i in key) {
  40. if (typeof i == "function")
  41. continue;
  42. this.data[i] = key[i];
  43. }
  44. } else {
  45. this.data[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
  46. }
  47. return this;
  48. },
  49. setData : function(data){
  50. this.data = data;
  51. return this;
  52. },
  53. setcontentType : function(data){
  54. this.contentType = data;
  55. return this;
  56. },
  57. clear : function () {
  58. this.data = {};
  59. return this;
  60. }
  61. };
  62. window.$ax = $ax;
  63. } ());