3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * Portions of this file are based on pieces of Yahoo User Interface Library
9 * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
10 * YUI licensed under the BSD License:
11 * http://developer.yahoo.net/yui/license.txt
13 Ext.lib.Ajax = function() {
14 var activeX = ['Msxml2.XMLHTTP.6.0',
17 CONTENTTYPE = 'Content-Type';
20 function setHeader(o) {
25 function setTheHeaders(conn, headers){
26 for (prop in headers) {
27 if (headers.hasOwnProperty(prop)) {
28 conn.setRequestHeader(prop, headers[prop]);
33 Ext.apply(headers, pub.headers, pub.defaultHeaders);
34 setTheHeaders(conn, headers);
39 function createExceptionObject(tId, callbackArg, isAbort, isTimeout) {
42 status : isAbort ? -1 : 0,
43 statusText : isAbort ? 'transaction aborted' : 'communication failure',
46 argument : callbackArg
51 function initHeader(label, value) {
52 (pub.headers = pub.headers || {})[label] = value;
56 function createResponseObject(o, callbackArg) {
62 // see: https://prototype.lighthouseapp.com/projects/8886/tickets/129-ie-mangles-http-response-status-code-204-to-1223
63 isBrokenStatus = conn.status == 1223;
66 headerStr = o.conn.getAllResponseHeaders();
67 Ext.each(headerStr.replace(/\r\n/g, '\n').split('\n'), function(v){
70 s = v.substr(0, t).toLowerCase();
71 if(v.charAt(t + 1) == ' '){
74 headerObj[s] = v.substr(t + 1);
81 // Normalize the status and statusText when IE returns 1223, see the above link.
82 status : isBrokenStatus ? 204 : conn.status,
83 statusText : isBrokenStatus ? 'No Content' : conn.statusText,
84 getResponseHeader : function(header){return headerObj[header.toLowerCase()];},
85 getAllResponseHeaders : function(){return headerStr;},
86 responseText : conn.responseText,
87 responseXML : conn.responseXML,
88 argument : callbackArg
93 function releaseObject(o) {
95 pub.conn[o.tId] = null;
102 function handleTransactionResponse(o, callback, isAbort, isTimeout) {
108 var httpStatus, responseObject;
111 if (o.conn.status !== undefined && o.conn.status != 0) {
112 httpStatus = o.conn.status;
122 if ((httpStatus >= 200 && httpStatus < 300) || (Ext.isIE && httpStatus == 1223)) {
123 responseObject = createResponseObject(o, callback.argument);
124 if (callback.success) {
125 if (!callback.scope) {
126 callback.success(responseObject);
129 callback.success.apply(callback.scope, [responseObject]);
134 switch (httpStatus) {
141 responseObject = createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false), isTimeout);
142 if (callback.failure) {
143 if (!callback.scope) {
144 callback.failure(responseObject);
147 callback.failure.apply(callback.scope, [responseObject]);
152 responseObject = createResponseObject(o, callback.argument);
153 if (callback.failure) {
154 if (!callback.scope) {
155 callback.failure(responseObject);
158 callback.failure.apply(callback.scope, [responseObject]);
165 responseObject = null;
168 function checkResponse(o, callback, conn, tId, poll, cbTimeout){
169 if (conn && conn.readyState == 4) {
170 clearInterval(poll[tId]);
174 clearTimeout(pub.timeout[tId]);
175 pub.timeout[tId] = null;
177 handleTransactionResponse(o, callback);
181 function checkTimeout(o, callback){
182 pub.abort(o, callback, true);
187 function handleReadyState(o, callback){
188 callback = callback || {};
192 cbTimeout = callback.timeout || null;
195 pub.conn[tId] = conn;
196 pub.timeout[tId] = setTimeout(checkTimeout.createCallback(o, callback), cbTimeout);
198 poll[tId] = setInterval(checkResponse.createCallback(o, callback, conn, tId, poll, cbTimeout), pub.pollInterval);
202 function asyncRequest(method, uri, callback, postData) {
203 var o = getConnectionObject() || null;
206 o.conn.open(method, uri, true);
208 if (pub.useDefaultXhrHeader) {
209 initHeader('X-Requested-With', pub.defaultXhrHeader);
212 if(postData && pub.useDefaultHeader && (!pub.headers || !pub.headers[CONTENTTYPE])){
213 initHeader(CONTENTTYPE, pub.defaultPostHeader);
216 if (pub.defaultHeaders || pub.headers) {
220 handleReadyState(o, callback);
221 o.conn.send(postData || null);
227 function getConnectionObject() {
231 if (o = createXhrObject(pub.transactionId)) {
241 function createXhrObject(transactionId) {
245 http = new XMLHttpRequest();
247 for (var i = 0; i < activeX.length; ++i) {
249 http = new ActiveXObject(activeX[i]);
254 return {conn : http, tId : transactionId};
259 request : function(method, uri, cb, data, options) {
262 xmlData = options.xmlData,
263 jsonData = options.jsonData,
266 Ext.applyIf(me, options);
268 if(xmlData || jsonData){
270 if(!hs || !hs[CONTENTTYPE]){
271 initHeader(CONTENTTYPE, xmlData ? 'text/xml' : 'application/json');
273 data = xmlData || (!Ext.isPrimitive(jsonData) ? Ext.encode(jsonData) : jsonData);
276 return asyncRequest(method || options.method || "POST", uri, cb, data);
279 serializeForm : function(form) {
280 var fElements = form.elements || (document.forms[form] || Ext.getDom(form)).elements,
282 encoder = encodeURIComponent,
288 Ext.each(fElements, function(element){
292 if (!element.disabled && name) {
293 if (/select-(one|multiple)/i.test(type)) {
294 Ext.each(element.options, function(opt){
296 hasValue = opt.hasAttribute ? opt.hasAttribute('value') : opt.getAttributeNode('value').specified;
297 data += String.format("{0}={1}&", encoder(name), encoder(hasValue ? opt.value : opt.text));
300 } else if (!(/file|undefined|reset|button/i.test(type))) {
301 if (!(/radio|checkbox/i.test(type) && !element.checked) && !(type == 'submit' && hasSubmit)) {
302 data += encoder(name) + '=' + encoder(element.value) + '&';
303 hasSubmit = /submit/i.test(type);
308 return data.substr(0, data.length - 1);
311 useDefaultHeader : true,
312 defaultPostHeader : 'application/x-www-form-urlencoded; charset=UTF-8',
313 useDefaultXhrHeader : true,
314 defaultXhrHeader : 'XMLHttpRequest',
321 // This is never called - Is it worth exposing this?
322 // setProgId : function(id) {
323 // activeX.unshift(id);
326 // This is never called - Is it worth exposing this?
327 // setDefaultPostHeader : function(b) {
328 // this.useDefaultHeader = b;
331 // This is never called - Is it worth exposing this?
332 // setDefaultXhrHeader : function(b) {
333 // this.useDefaultXhrHeader = b;
336 // This is never called - Is it worth exposing this?
337 // setPollingInterval : function(i) {
338 // if (typeof i == 'number' && isFinite(i)) {
339 // this.pollInterval = i;
343 // This is never called - Is it worth exposing this?
344 // resetDefaultHeaders : function() {
345 // this.defaultHeaders = null;
348 abort : function(o, callback, isTimeout) {
353 if (me.isCallInProgress(o)) {
355 clearInterval(me.poll[tId]);
357 clearTimeout(pub.timeout[tId]);
358 me.timeout[tId] = null;
360 handleTransactionResponse(o, callback, (isAbort = true), isTimeout);
365 isCallInProgress : function(o) {
366 // if there is a connection and readyState is not 0 or 4
367 return o.conn && !{0:true,4:true}[o.conn.readyState];