3 * Copyright(c) 2006-2010 Ext JS, LLC
5 * http://www.extjs.com/license
8 * Portions of this file are based on pieces of Yahoo User Interface Library
\r
9 * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
\r
10 * YUI licensed under the BSD License:
\r
11 * http://developer.yahoo.net/yui/license.txt
\r
13 Ext.lib.Ajax = function() {
\r
14 var activeX = ['MSXML2.XMLHTTP.3.0',
\r
16 'Microsoft.XMLHTTP'],
\r
17 CONTENTTYPE = 'Content-Type';
\r
20 function setHeader(o) {
\r
24 function setTheHeaders(conn, headers){
\r
25 for (prop in headers) {
\r
26 if (headers.hasOwnProperty(prop)) {
\r
27 conn.setRequestHeader(prop, headers[prop]);
\r
32 if (pub.defaultHeaders) {
\r
33 setTheHeaders(conn, pub.defaultHeaders);
\r
37 setTheHeaders(conn, pub.headers);
\r
38 delete pub.headers;
\r
43 function createExceptionObject(tId, callbackArg, isAbort, isTimeout) {
\r
46 status : isAbort ? -1 : 0,
\r
47 statusText : isAbort ? 'transaction aborted' : 'communication failure',
\r
49 isTimeout: isTimeout,
\r
50 argument : callbackArg
\r
55 function initHeader(label, value) {
\r
56 (pub.headers = pub.headers || {})[label] = value;
\r
60 function createResponseObject(o, callbackArg) {
\r
68 headerStr = o.conn.getAllResponseHeaders();
\r
69 Ext.each(headerStr.replace(/\r\n/g, '\n').split('\n'), function(v){
\r
72 s = v.substr(0, t).toLowerCase();
\r
73 if(v.charAt(t + 1) == ' '){
\r
76 headerObj[s] = v.substr(t + 1);
\r
83 status : conn.status,
\r
84 statusText : conn.statusText,
\r
85 getResponseHeader : function(header){return headerObj[header.toLowerCase()];},
\r
86 getAllResponseHeaders : function(){return headerStr},
\r
87 responseText : conn.responseText,
\r
88 responseXML : conn.responseXML,
\r
89 argument : callbackArg
\r
94 function releaseObject(o) {
\r
100 function handleTransactionResponse(o, callback, isAbort, isTimeout) {
\r
106 var httpStatus, responseObject;
\r
109 if (o.conn.status !== undefined && o.conn.status != 0) {
\r
110 httpStatus = o.conn.status;
\r
113 httpStatus = 13030;
\r
117 httpStatus = 13030;
\r
120 if ((httpStatus >= 200 && httpStatus < 300) || (Ext.isIE && httpStatus == 1223)) {
\r
121 responseObject = createResponseObject(o, callback.argument);
\r
122 if (callback.success) {
\r
123 if (!callback.scope) {
\r
124 callback.success(responseObject);
\r
127 callback.success.apply(callback.scope, [responseObject]);
\r
132 switch (httpStatus) {
\r
139 responseObject = createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false), isTimeout);
\r
140 if (callback.failure) {
\r
141 if (!callback.scope) {
\r
142 callback.failure(responseObject);
\r
145 callback.failure.apply(callback.scope, [responseObject]);
\r
150 responseObject = createResponseObject(o, callback.argument);
\r
151 if (callback.failure) {
\r
152 if (!callback.scope) {
\r
153 callback.failure(responseObject);
\r
156 callback.failure.apply(callback.scope, [responseObject]);
\r
163 responseObject = null;
\r
167 function handleReadyState(o, callback){
\r
168 callback = callback || {};
\r
172 cbTimeout = callback.timeout || null;
\r
175 pub.timeout[tId] = setTimeout(function() {
\r
176 pub.abort(o, callback, true);
\r
180 poll[tId] = setInterval(
\r
182 if (conn && conn.readyState == 4) {
\r
183 clearInterval(poll[tId]);
\r
187 clearTimeout(pub.timeout[tId]);
\r
188 pub.timeout[tId] = null;
\r
191 handleTransactionResponse(o, callback);
\r
198 function asyncRequest(method, uri, callback, postData) {
\r
199 var o = getConnectionObject() || null;
\r
202 o.conn.open(method, uri, true);
\r
204 if (pub.useDefaultXhrHeader) {
\r
205 initHeader('X-Requested-With', pub.defaultXhrHeader);
\r
208 if(postData && pub.useDefaultHeader && (!pub.headers || !pub.headers[CONTENTTYPE])){
\r
209 initHeader(CONTENTTYPE, pub.defaultPostHeader);
\r
212 if (pub.defaultHeaders || pub.headers) {
\r
216 handleReadyState(o, callback);
\r
217 o.conn.send(postData || null);
\r
223 function getConnectionObject() {
\r
227 if (o = createXhrObject(pub.transactionId)) {
\r
228 pub.transactionId++;
\r
237 function createXhrObject(transactionId) {
\r
241 http = new XMLHttpRequest();
\r
243 for (var i = 0; i < activeX.length; ++i) {
\r
245 http = new ActiveXObject(activeX[i]);
\r
250 return {conn : http, tId : transactionId};
\r
255 request : function(method, uri, cb, data, options) {
\r
258 xmlData = options.xmlData,
\r
259 jsonData = options.jsonData,
\r
262 Ext.applyIf(me, options);
\r
264 if(xmlData || jsonData){
\r
266 if(!hs || !hs[CONTENTTYPE]){
\r
267 initHeader(CONTENTTYPE, xmlData ? 'text/xml' : 'application/json');
\r
269 data = xmlData || (!Ext.isPrimitive(jsonData) ? Ext.encode(jsonData) : jsonData);
\r
272 return asyncRequest(method || options.method || "POST", uri, cb, data);
\r
275 serializeForm : function(form) {
\r
276 var fElements = form.elements || (document.forms[form] || Ext.getDom(form)).elements,
\r
278 encoder = encodeURIComponent,
\r
286 Ext.each(fElements, function(element) {
\r
287 name = element.name;
\r
288 type = element.type;
\r
290 if (!element.disabled && name){
\r
291 if(/select-(one|multiple)/i.test(type)) {
\r
292 Ext.each(element.options, function(opt) {
\r
293 if (opt.selected) {
\r
294 data += String.format("{0}={1}&", encoder(name), encoder((opt.hasAttribute ? opt.hasAttribute('value') : opt.getAttribute('value') !== null) ? opt.value : opt.text));
\r
297 } else if(!/file|undefined|reset|button/i.test(type)) {
\r
298 if(!(/radio|checkbox/i.test(type) && !element.checked) && !(type == 'submit' && hasSubmit)){
\r
300 data += encoder(name) + '=' + encoder(element.value) + '&';
\r
301 hasSubmit = /submit/i.test(type);
\r
306 return data.substr(0, data.length - 1);
\r
309 useDefaultHeader : true,
\r
310 defaultPostHeader : 'application/x-www-form-urlencoded; charset=UTF-8',
\r
311 useDefaultXhrHeader : true,
\r
312 defaultXhrHeader : 'XMLHttpRequest',
\r
318 // This is never called - Is it worth exposing this?
\r
319 // setProgId : function(id) {
\r
320 // activeX.unshift(id);
\r
323 // This is never called - Is it worth exposing this?
\r
324 // setDefaultPostHeader : function(b) {
\r
325 // this.useDefaultHeader = b;
\r
328 // This is never called - Is it worth exposing this?
\r
329 // setDefaultXhrHeader : function(b) {
\r
330 // this.useDefaultXhrHeader = b;
\r
333 // This is never called - Is it worth exposing this?
\r
334 // setPollingInterval : function(i) {
\r
335 // if (typeof i == 'number' && isFinite(i)) {
\r
336 // this.pollInterval = i;
\r
340 // This is never called - Is it worth exposing this?
\r
341 // resetDefaultHeaders : function() {
\r
342 // this.defaultHeaders = null;
\r
345 abort : function(o, callback, isTimeout) {
\r
350 if (me.isCallInProgress(o)) {
\r
352 clearInterval(me.poll[tId]);
\r
353 me.poll[tId] = null;
\r
354 clearTimeout(pub.timeout[tId]);
\r
355 me.timeout[tId] = null;
\r
357 handleTransactionResponse(o, callback, (isAbort = true), isTimeout);
\r
362 isCallInProgress : function(o) {
\r
363 // if there is a connection and readyState is not 0 or 4
\r
364 return o.conn && !{0:true,4:true}[o.conn.readyState];
\r