4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 * This class enables you to send XMLHTTPRequests cross browser.
13 * @class tinymce.util.XHR
16 * // Sends a low level Ajax request
17 * tinymce.util.XHR.send({
19 * success : function(text) {
20 * console.debug(text);
24 tinymce.create('static tinymce.util.XHR', {
26 * Sends a XMLHTTPRequest.
27 * Consult the Wiki for details on what settings this method takes.
30 * @param {Object} o Object will target URL, callbacks and other info needed to make the request.
33 var x, t, w = window, c = 0;
36 o.scope = o.scope || this;
37 o.success_scope = o.success_scope || o.scope;
38 o.error_scope = o.error_scope || o.scope;
39 o.async = o.async === false ? false : true;
40 o.data = o.data || '';
46 x = new ActiveXObject(s);
53 x = w.XMLHttpRequest ? new XMLHttpRequest() : get('Microsoft.XMLHTTP') || get('Msxml2.XMLHTTP');
56 if (x.overrideMimeType)
57 x.overrideMimeType(o.content_type);
59 x.open(o.type || (o.data ? 'POST' : 'GET'), o.url, o.async);
62 x.setRequestHeader('Content-Type', o.content_type);
64 x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
69 if (!o.async || x.readyState == 4 || c++ > 10000) {
70 if (o.success && c < 10000 && x.status == 200)
71 o.success.call(o.success_scope, '' + x.responseText, x, o);
73 o.error.call(o.error_scope, c > 10000 ? 'TIMED_OUT' : 'GENERAL', x, o);
77 w.setTimeout(ready, 10);
84 // Wait for response, onReadyStateChange can not be used since it leaks memory in IE
85 t = w.setTimeout(ready, 10);