ajax.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * XmlHttpRequest객체를 생성하고 이 객체를 이용해 ajax request를 수행한다.
  3. * @class
  4. */
  5. Trex.I.XHRequester = Trex.Faculty.create(/** @lends Trex.I.XHRequester */{
  6. /**
  7. * 브라우져에 맞는 XmlHttpRequest 객체를 생성해서 리턴한다.
  8. * @private
  9. * @return {Object} XmlHttpRequest object
  10. */
  11. createXMLHttp: function() {
  12. var _xmlHttp = _NULL;
  13. try{
  14. if(_WIN.XMLHttpRequest) {
  15. _xmlHttp = new XMLHttpRequest();
  16. } else if (_WIN.ActiveXObject) {
  17. _xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  18. if(!_xmlHttp) {
  19. _xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
  20. }
  21. }
  22. return _xmlHttp;
  23. }catch(e){
  24. return _NULL;
  25. }
  26. },
  27. /**
  28. * ajax call를 수행한다
  29. * @param {String} method - http request의 방식, "get" 또는 "post"
  30. * @param {String} url - request를 날릴 url
  31. * @param {Boolean} async - synchronous 여부
  32. * @param {Function} successHandler - ajax의 성공시의 핸들러
  33. * @param {Function} failHandler - ajax 실패시이의 핸들러
  34. * @example
  35. * this.sendRequest("get","http://www.daum.net/api",true,function(value){alert(value)}, function(){alert('fail');}
  36. */
  37. sendRequest: function(method, url, param, async, successHandler, failHandler) {
  38. if (url == _NULL && url != "") {
  39. return _NULL;
  40. }
  41. var _response = _NULL;
  42. var _xmlHttp = this.createXMLHttp();
  43. if(_xmlHttp == _NULL) {
  44. return _NULL;
  45. }
  46. var handler = function(){
  47. if (_xmlHttp.status == 200) {
  48. if (method.toUpperCase() == "HEAD") {
  49. _response = successHandler(_xmlHttp.getAllResponseHeaders());
  50. } else {
  51. _response = successHandler(_xmlHttp.responseText);
  52. }
  53. } else {
  54. _response = failHandler(_xmlHttp.status);
  55. }
  56. _xmlHttp = _NULL;
  57. };
  58. try{
  59. if (async) {
  60. _xmlHttp.onreadystatechange = function(){
  61. if (_xmlHttp.readyState == 4) {
  62. handler();
  63. }
  64. };
  65. }
  66. if(method.toUpperCase() == "POST") {
  67. _xmlHttp.open("POST", url, async);
  68. _xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
  69. _xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  70. //webkit 오류발생
  71. // _xmlHttp.setRequestHeader("Content-Length", param.length);
  72. // _xmlHttp.setRequestHeader("Connetion","close");
  73. _xmlHttp.send(param);
  74. } else {
  75. if(param && param.length > 0) {
  76. url = url + ((url.indexOf("?") > -1)? "&": "?") + param;
  77. }
  78. _xmlHttp.open(method.toUpperCase(), url, async);
  79. _xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  80. _xmlHttp.send(_NULL);
  81. }
  82. if (!async) {
  83. handler();
  84. }
  85. return _response;
  86. }catch(e){
  87. return _NULL;
  88. }
  89. }
  90. });
  91. Trex.Responder = {
  92. callbacks: {},
  93. process: function(/*bytesLoaded, bytesTotal*/) {
  94. //if(bytesLoaded < 0) {
  95. // fail
  96. //} else {
  97. // progress
  98. //}
  99. },
  100. newKey: function() {
  101. var _key = "exe_" + Math.floor(Math.random() * 100000);
  102. if(this[_key]) {
  103. return this.newKey();
  104. } else {
  105. return _key;
  106. }
  107. },
  108. register: function(handler) {
  109. var _key = this.newKey();
  110. this.callbacks[_key] = function(response) {
  111. handler.apply(this, Array.prototype.slice.call(arguments,0));
  112. this.callbacks[_key] = _NULL;
  113. }.bind(this);
  114. return _key;
  115. }
  116. };
  117. /**
  118. * 동적으로 외부의 javascript파일을 include한다.
  119. * @class
  120. */
  121. Trex.I.JSRequester = Trex.Faculty.create(/** @lends Trex.I.JSRequester */{
  122. /**
  123. * 특정위치의 스크립트 파일을 include 한다.
  124. * @param {String} url - http request의 방식, "get" 또는 "post"
  125. * @param {String} encoding - inlcude할 javascript의 encoding 타입
  126. * @param {Element} context - 로딩된 스크립트가 표시될 dom element
  127. * @param {Function} success - ajax의 성공시의 핸들러
  128. * @example
  129. * this.importScript("http://www.daum.net/api/movie.js?apikey=1234","utf-8", document, function(){alert("hello");} )
  130. */
  131. importScript: function(url, encoding, context, success) {
  132. if (url == _NULL && url != "") {
  133. return _NULL;
  134. }
  135. encoding = encoding || "utf-8";
  136. context = context || _DOC;
  137. try{
  138. var head = context.getElementsByTagName("head")[0] || context.documentElement;
  139. var script = context.createElement("script");
  140. script.type = "text/javascript";
  141. script.charset = encoding;
  142. script.src= url;
  143. var done = _FALSE;
  144. script.onload = script.onreadystatechange = function() {
  145. if ( !done && (!this.readyState ||
  146. this.readyState === "loaded" || this.readyState === "complete") ) {
  147. done = _TRUE;
  148. if (success) {
  149. success();
  150. }
  151. // Handle memory leak in IE
  152. script.onload = script.onreadystatechange = _NULL;
  153. if ( head && script.parentNode ) {
  154. head.removeChild( script );
  155. }
  156. }
  157. };
  158. head.insertBefore( script, head.firstChild );
  159. }catch(e){
  160. console.log(e)
  161. }
  162. }
  163. });