xmlhttp.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * Ajax(Asynchronous JavaScript + XML)
  3. * XmlHttpRequest를 이용해 WAS와 통신할 수 있는 기능을 제공
  4. */
  5. /**
  6. * Debug 활성화
  7. */
  8. var xmlhttp_debug = false;
  9. /**
  10. * XmlHttpRequest 객체 생성
  11. */
  12. function createXmlHttpRequest() {
  13. var xmlHttpRequest = null;
  14. try {
  15. if (window.XMLHttpRequest) {
  16. // If IE7, Mozilla, Safari, and so on: Use native object
  17. xmlHttpRequest = new XMLHttpRequest();
  18. } else {
  19. xmlHttpRequest = getXxmlHttpRequest();
  20. }
  21. return xmlHttpRequest;
  22. } catch (errorObject) {
  23. showErrorDlg('createXmlHttpRequest()', errorObject);
  24. }
  25. }
  26. /**
  27. * XmlHttpRequest 객체 생성
  28. */
  29. function getXxmlHttpRequest() {
  30. var xmlhttps = new Array("Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0",
  31. "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0",
  32. "Msxml2.XMLHTTP", "Microsoft.XMLHTTP");
  33. for (var i = 0; i < xmlhttps.length; i++) {
  34. try {
  35. var obj = new ActiveXObject(xmlhttps[i]);
  36. return obj;
  37. } catch (errorObject) {
  38. }
  39. }
  40. alert("귀하에 PC에는 XmlHttp Objec가 설치되어 있지 않습니다.\n\n자세한 내용은 전산실로 문의하시기 바랍니다.");
  41. return null;
  42. }
  43. /**
  44. * 데이터를 WAS로 전송 actionUrl : 데이터가 전송 되어질 URL or URI event : 전달 Event formObject :
  45. * 전달될 데이터(Form element of HTML)
  46. */
  47. function submitXmlRequest(actionUrl, event, formObject, resultType, isLockForm) {
  48. if (isLockForm == null)
  49. isLockForm = true;
  50. if (formObject == null)
  51. return false;
  52. if (formObject.isSubmit == true || formObject.isSubmit == "true") {
  53. return false;
  54. }
  55. if (formObject != null && isLockForm == true)
  56. formObject.isSubmit = true;
  57. var xmlHttpRequest = createXmlHttpRequest();
  58. try {
  59. xmlHttpRequest.open('POST', actionUrl, true);
  60. xmlHttpRequest.setRequestHeader('Content-Type',
  61. 'application/x-www-form-urlencoded;');
  62. xmlHttpRequest.setRequestHeader('Request-Type', 'XmlHttpRequest');
  63. xmlHttpRequest.onreadystatechange = function() {
  64. if (xmlHttpRequest.readyState == 4) {
  65. hideLoadingBar(); //중복 요청을 막기 위하여 로딩바 추가 2019.08.23
  66. switch (xmlHttpRequest.status) {
  67. case 200:
  68. if (xmlhttp_debug)
  69. alert(xmlHttpRequest.responseText);
  70. parseXml(xmlHttpRequest.responseXML,
  71. xmlHttpRequest.responseText);
  72. break;
  73. default:
  74. var msg = "";
  75. msg += "URL : " + actionUrl + "\n\n";
  76. msg += "Status : " + xmlHttpRequest.status + "\n\n";
  77. msg += "Text : " + xmlHttpRequest.statusText + "\n\n";
  78. alert(msg);
  79. break;
  80. }
  81. }
  82. };
  83. var data = formData2QueryString(formObject);
  84. if (resultType != null)
  85. data += "&result_type=" + resultType;
  86. if (xmlhttp_debug)
  87. alert("fevent=" + URLEncode(event) + "&" + data);
  88. xmlHttpRequest.send("fevent=" + URLEncode(event) + "&" + data);
  89. showLoadingBar(); //중복 요청을 막기 위하여 로딩바 추가 2019.08.23
  90. return true;
  91. } catch (errorObject) {
  92. showErrorDlg("submitXmlRequest()", errorObject);
  93. formObject.isSubmit = false;
  94. return false;
  95. }
  96. }
  97. /**
  98. * Form Object의 데이터값을 추출하여 리턴 (application/x-www-form-urlencoded MIME 형식으로 변환)
  99. */
  100. function formData2QueryString(formObject) {
  101. if (formObject == null)
  102. return "";
  103. var submitString = '';
  104. var formElement = '';
  105. //var lastElementName = '';
  106. try {
  107. for (var i = 0; i < formObject.elements.length; i++) {
  108. formElement = formObject.elements[i];
  109. if (formElement.name == "fevent")
  110. continue;
  111. if (formElement.is_html_editor == "true") {
  112. formElement.value = getHTMLValue(formElement.name);
  113. }
  114. switch (formElement.type) {
  115. case 'text':
  116. case 'select-one':
  117. case 'hidden':
  118. case 'password':
  119. case 'textarea':
  120. submitString += formElement.name + '='
  121. + URLEncode(formElement.value) + '&';
  122. break;
  123. case 'radio':
  124. if (formElement.checked) {
  125. submitString += formElement.name + '='
  126. + URLEncode(formElement.value) + '&';
  127. }
  128. break;
  129. case 'checkbox':
  130. if (formElement.checked) {
  131. // if(formElement.name = lastElementName) {
  132. // if(submitString.lastIndexOf('&') == submitString.length -
  133. // 1) {
  134. // submitString = submitString.substring(0,
  135. // submitString.length - 1);
  136. // }
  137. // submitString += ',' + URLEncode(formElement.value);
  138. // }
  139. // else {
  140. submitString += formElement.name + '='
  141. + URLEncode(formElement.value);
  142. // }
  143. submitString += '&';
  144. // lastElementName = formElement.name;
  145. }
  146. break;
  147. case 'select-multiple':
  148. for (var j = 0; j < formElement.length; j++) {
  149. if (formElement.options[j].selected == true) {
  150. submitString += formElement.name + '='
  151. + URLEncode(formElement.options[j].value) + '&';
  152. }
  153. }
  154. break;
  155. }
  156. }
  157. submitString = submitString.substring(0, submitString.length - 1);
  158. return submitString;
  159. } catch (errorObject) {
  160. showErrorDlg("formData2QueryString()", errorObject);
  161. }
  162. }
  163. /**
  164. * String을 application/x-www-form-urlencoded MIME 형식으로 변환
  165. */
  166. function URLEncode(str) {
  167. return encodeURIComponent(str);
  168. }
  169. /**
  170. * 결과메세지를 Parsing 처리 return : Result(result.js 참조)
  171. */
  172. function parseXml(responseXML, responseText, resultType) {
  173. try {
  174. var resultNodeList = responseXML.getElementsByTagName("RESULT");
  175. var rootNode = resultNodeList.item(0);
  176. var resultType = rootNode.getAttribute("type");
  177. var requestNodeList = rootNode.getElementsByTagName("REQUEST");
  178. var requestNode = requestNodeList.item(0);
  179. var requestUri = requestNode.getAttribute("uri");
  180. var requestEvent = requestNode.getAttribute("event");
  181. var requestParameter = rootNode.getElementsByTagName("REQUEST").item(0).text;
  182. var success = rootNode.getElementsByTagName("SUCCESS").item(0).getAttribute("value");
  183. //var message = rootNode.getElementsByTagName("SUCCESS").item(0).text;
  184. var message = rootNode.getElementsByTagName("SUCCESS").item(0).childNodes[0].nodeValue;
  185. var detailMessage = "";
  186. if (rootNode.getElementsByTagName("DETAIL-MESSAGE").item(0) != null)
  187. detailMessage = rootNode.getElementsByTagName("DETAIL-MESSAGE").item(0).childNodes[0].nodeValue;
  188. //detailMessage = rootNode.getElementsByTagName("DETAIL-MESSAGE").item(0).text;
  189. var resultObject = new Result(resultType, requestUri, requestEvent);
  190. resultObject.setRequestParameter(requestParameter);
  191. if (success.toLowerCase() == "true") {
  192. success = true;
  193. } else if (success.toLowerCase() == "false") {
  194. success = false;
  195. }
  196. resultObject.setSuccess(success);
  197. resultObject.setMessage(message);
  198. resultObject.setDetailMessage(detailMessage);
  199. if (resultType.toLowerCase() == "success") {
  200. // 추가 Parsing 없음
  201. } else if (resultType.toLowerCase() == "select") {
  202. var voNode = rootNode.getElementsByTagName("VALUE-OBJECT").item(0);
  203. var vo = new ValueObject();
  204. var size = voNode.getAttribute("size");
  205. for (var i = 0; i < size; i++) {
  206. var key = voNode.getElementsByTagName("VALUE").item(i)
  207. .getAttribute("name");
  208. var value = voNode.getElementsByTagName("VALUE").item(i).text;
  209. vo.set(key, value);
  210. }
  211. resultObject.setSelect(vo);
  212. } else if (resultType.toLowerCase() == "valueobject") {
  213. var voNode = rootNode.getElementsByTagName("VALUE-OBJECT").item(0);
  214. var vo = new ValueObject();
  215. var size = voNode.getAttribute("size");
  216. for (var i = 0; i < size; i++) {
  217. var key = voNode.getElementsByTagName("VALUE").item(i)
  218. .getAttribute("name");
  219. var value = voNode.getElementsByTagName("VALUE").item(i).childNodes[0].nodeValue;
  220. vo.set(key, value);
  221. }
  222. resultObject.setValueObject(vo);
  223. } else if (resultType.toLowerCase() == "rowset") {
  224. var rowSetNode = rootNode.getElementsByTagName("ROW-SET").item(0);
  225. var colSize = rowSetNode.getAttribute("cols");
  226. var rowSize = rowSetNode.getAttribute("rows");
  227. var rowSet = new RowSet();
  228. for (var i = 0; i < rowSize; i++) {
  229. var rowNode = rowSetNode.getElementsByTagName("ROW").item(i);
  230. //var rowIndex = rowNode.getAttribute("index");
  231. var row = new Row();
  232. for (var j = 0; j < colSize; j++) {
  233. var columnNode = rowNode.getElementsByTagName("COLUMN")
  234. .item(j);
  235. var name = columnNode.getAttribute("name");
  236. var value = columnNode.childNodes[0].nodeValue;
  237. row.set(name, value);
  238. }
  239. rowSet.setRow(row);
  240. }
  241. resultObject.setRowSet(rowSet);
  242. } else if (resultType.toLowerCase() == "xml") {
  243. var xml = rootNode.getElementsByTagName("XML").item(0).xml;
  244. resultObject.setXml(xml);
  245. } else {
  246. alert("알수 없는 메세지 타입 입니다.\n\ntype : " + resultType);
  247. }
  248. try {
  249. if (doResult != null) {
  250. doResult(resultObject);
  251. } else {
  252. alert("doResult() 함수가 없습니다.");
  253. }
  254. } catch (errorObject) {
  255. showErrorDlg("doResult()", errorObject);
  256. }
  257. } catch (errorObject) {
  258. alert("XML을 분석할 수없습니다.\n\nXML :\n" + responseText);
  259. showErrorDlg("parseXml()", errorObject);
  260. }
  261. }
  262. /**
  263. * 데이터를 WAS로 전송 actionUrl : 데이터가 전송 되어질 URL or URI event : 전달 Event formObject :
  264. * 전달될 데이터(Form element of HTML)
  265. */
  266. function submitXmlHttpRequest(actionUrl, event, formObject, isLockForm,
  267. resultType) {
  268. if (isLockForm == null)
  269. isLockForm = true;
  270. if (formObject == null)
  271. return false;
  272. if (formObject.isSubmit == true || formObject.isSubmit == "true") {
  273. return false;
  274. }
  275. if (formObject != null && isLockForm == true)
  276. formObject.isSubmit = true;
  277. try {
  278. xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  279. } catch (e) {
  280. try {
  281. xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  282. } catch (e2) {
  283. xmlHttpRequest = false;
  284. }
  285. }
  286. if (!xmlHttpRequest && typeof XMLHttpRequest != 'undefined') {
  287. try {
  288. xmlHttpRequest = new XMLHttpRequest();
  289. } catch (failed) {
  290. xmlHttpRequest = false;
  291. }
  292. }
  293. if (!xmlHttpRequest)
  294. alert("Error initializing XMLHttpRequest!");
  295. try {
  296. xmlHttpRequest.open('POST', actionUrl, true);
  297. xmlHttpRequest.setRequestHeader('Content-Type',
  298. 'application/x-www-form-urlencoded;');
  299. xmlHttpRequest.setRequestHeader('Request-Type', 'XmlHttpRequest');
  300. xmlHttpRequest.onreadystatechange = function() {
  301. if (xmlHttpRequest.readyState == 4) {
  302. hideLoadingBar(); //중복 요청을 막기 위하여 로딩바 추가 2019.08.23
  303. switch (xmlHttpRequest.status) {
  304. case 200:
  305. var responseText = xmlHttpRequest.responseText.replace(
  306. /^\s\s*/, '').replace(/\s\s*$/, '');
  307. var resultObject = new Result(resultType, actionUrl, event);
  308. resultObject.setXml(responseText);
  309. resultObject.setSuccess(true);
  310. doResult(resultObject);
  311. break;
  312. default:
  313. var msg = "";
  314. msg += "URL : " + actionUrl + "\n\n";
  315. msg += "Status : " + xmlHttpRequest.status + "\n\n";
  316. msg += "Text : " + xmlHttpRequest.statusText + "\n\n";
  317. alert(msg);
  318. break;
  319. }
  320. }
  321. };
  322. var data = formData2QueryString(formObject);
  323. if (resultType != null)
  324. data += "&result_type=" + resultType;
  325. if (xmlhttp_debug)
  326. alert("fevent=" + URLEncode(event) + "&" + data);
  327. xmlHttpRequest.send("fevent=" + URLEncode(event) + "&" + data);
  328. showLoadingBar(); //중복 요청을 막기 위하여 로딩바 추가 2019.08.23
  329. return true;
  330. } catch (errorObject) {
  331. showErrorDlg("submitXmlRequest()", errorObject);
  332. formObject.isSubmit = false;
  333. return false;
  334. }
  335. }