localXHR.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. Ext.apply( Ext.lib.Ajax ,
  2. { forceActiveX:false,
  3. createXhrObject:function(transactionId)
  4. {
  5. var obj,http;
  6. try
  7. {
  8. if(Ext.isIE7 && !!this.forceActiveX){throw("IE7forceActiveX");}
  9. http = new XMLHttpRequest();
  10. obj = { conn:http, tId:transactionId };
  11. }
  12. catch(e)
  13. {
  14. for (var i = 0; i < this.activeX.length; ++i) {
  15. try
  16. {
  17. http = new ActiveXObject(this.activeX[i]);
  18. obj = { conn:http, tId:transactionId };
  19. break;
  20. }
  21. catch(e) {
  22. }
  23. }
  24. }
  25. finally
  26. {
  27. return obj;
  28. }
  29. },
  30. getHttpStatus: function(reqObj){
  31. var statObj = { status:0
  32. ,statusText:''
  33. ,isError:false
  34. ,isLocal:false
  35. ,isOK:false };
  36. try {
  37. if(!reqObj)throw('noobj');
  38. statObj.status = reqObj.status || 0;
  39. statObj.isLocal = !reqObj.status && location.protocol == "file:" ||
  40. Ext.isSafari && reqObj.status == undefined;
  41. statObj.statusText = reqObj.statusText || '';
  42. statObj.isOK = (statObj.isLocal ||
  43. (statObj.status > 199 && statObj.status < 300) ||
  44. statObj.status == 304);
  45. } catch(e){ statObj.isError = true;} //status may not avail/valid yet.
  46. return statObj;
  47. },
  48. handleTransactionResponse:function(o, callback, isAbort)
  49. {
  50. var responseObject;
  51. callback = callback || {};
  52. o.status = this.getHttpStatus(o.conn);
  53. if(!o.status.isError){
  54. /* create and enhance the response with proper status and XMLDOM if necessary */
  55. responseObject = this.createResponseObject(o, callback.argument);
  56. }
  57. if(o.status.isError){ /* checked again in case exception was raised - ActiveX was disabled during XML-DOM creation? */
  58. responseObject = this.createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false));
  59. }
  60. if (o.status.isOK && !o.status.isError) {
  61. if (callback.success) {
  62. if (!callback.scope) {
  63. callback.success(responseObject);
  64. }
  65. else {
  66. callback.success.apply(callback.scope, [responseObject]);
  67. }
  68. }
  69. } else {
  70. if (callback.failure) {
  71. if (!callback.scope) {
  72. callback.failure(responseObject);
  73. }
  74. else {
  75. callback.failure.apply(callback.scope, [responseObject]);
  76. }
  77. }
  78. }
  79. this.releaseObject(o);
  80. responseObject = null;
  81. },
  82. createResponseObject:function(o, callbackArg)
  83. {
  84. var obj = {};
  85. var headerObj = {};
  86. try
  87. {
  88. var headerStr = o.conn.getAllResponseHeaders();
  89. var header = headerStr.split('\n');
  90. for (var i = 0; i < header.length; i++) {
  91. var delimitPos = header[i].indexOf(':');
  92. if (delimitPos != -1) {
  93. headerObj[header[i].substring(0, delimitPos)] = header[i].substring(delimitPos + 2);
  94. }
  95. }
  96. }
  97. catch(e) {
  98. }
  99. obj.tId = o.tId;
  100. obj.status = o.status.status;
  101. obj.statusText = o.status.statusText;
  102. obj.getResponseHeader = headerObj;
  103. obj.getAllResponseHeaders = headerStr;
  104. obj.responseText = o.conn.responseText;
  105. obj.responseXML = o.conn.responseXML;
  106. if(o.status.isLocal){
  107. o.status.isOK = ((obj.status = o.status.status = (!!obj.responseText.length)?200:404) == 200);
  108. if(o.status.isOK && (!obj.responseXML || obj.responseXML.childNodes.length == 0)){
  109. var xdoc=null;
  110. try{ //ActiveX may be disabled
  111. if(typeof(DOMParser) == 'undefined'){
  112. xdoc=new ActiveXObject("Microsoft.XMLDOM");
  113. xdoc.async="false";
  114. xdoc.loadXML(obj.responseText);
  115. }else{
  116. var domParser = new DOMParser();
  117. xdoc = domParser.parseFromString(obj.responseText, 'application/xml');
  118. domParser = null;
  119. }
  120. } catch(ex){
  121. o.status.isError = true;
  122. }
  123. obj.responseXML = xdoc;
  124. if ( xdoc && typeof (obj.getResponseHeader['Content-Type']) == 'undefined' &&
  125. !!xdoc.childNodes.length ) /* Got valid nodes? then set the response header */
  126. {
  127. obj.getResponseHeader['Content-Type'] == 'text/xml';
  128. }
  129. }
  130. }
  131. if (typeof callbackArg !== undefined) {
  132. obj.argument = callbackArg;
  133. }
  134. return obj;
  135. },
  136. asyncRequest:function(method, uri, callback, postData)
  137. {
  138. var o = this.getConnectionObject();
  139. if (!o) {
  140. return null;
  141. }
  142. else {
  143. try{
  144. o.conn.open(method, uri, true);
  145. } catch(ex){
  146. this.handleTransactionResponse(o, callback);
  147. return o;
  148. }
  149. if (this.useDefaultXhrHeader) {
  150. if (!this.defaultHeaders['X-Requested-With']) {
  151. this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
  152. }
  153. }
  154. if(postData && this.useDefaultHeader){
  155. this.initHeader('Content-Type', this.defaultPostHeader);
  156. }
  157. if (this.hasDefaultHeaders || this.hasHeaders) {
  158. this.setHeader(o);
  159. }
  160. this.handleReadyState(o, callback);
  161. try{ o.conn.send(postData || null);
  162. } catch(ex){ this.handleTransactionResponse(o, callback);}
  163. return o;
  164. }
  165. }});
  166. Ext.lib.Ajax.forceActiveX = (document.location.protocol == 'file:');/* or other true/false mechanism */