|
20 | 20 | * @singleton
|
21 | 21 | */
|
22 | 22 | CKEDITOR.ajax = ( function() {
|
23 |
| - var createXMLHttpRequest = function() { |
24 |
| - // In IE, using the native XMLHttpRequest for local files may throw |
25 |
| - // "Access is Denied" errors. |
26 |
| - if ( !CKEDITOR.env.ie || location.protocol != 'file:' ) |
27 |
| - try { |
28 |
| - return new XMLHttpRequest(); |
29 |
| - } catch ( e ) {} |
30 |
| - |
31 |
| - try { |
32 |
| - return new ActiveXObject( 'Msxml2.XMLHTTP' ); |
33 |
| - } catch ( e ) {} |
| 23 | + function createXMLHttpRequest() { |
| 24 | + // In IE, using the native XMLHttpRequest for local files may throw |
| 25 | + // "Access is Denied" errors. |
| 26 | + if ( !CKEDITOR.env.ie || location.protocol != 'file:' ) |
34 | 27 | try {
|
35 |
| - return new ActiveXObject( 'Microsoft.XMLHTTP' ); |
36 |
| - } catch ( e ) {} |
| 28 | + return new XMLHttpRequest(); |
| 29 | + } catch ( e ) {} |
| 30 | + |
| 31 | + try { |
| 32 | + return new ActiveXObject( 'Msxml2.XMLHTTP' ); |
| 33 | + } catch ( e ) {} |
| 34 | + try { |
| 35 | + return new ActiveXObject( 'Microsoft.XMLHTTP' ); |
| 36 | + } catch ( e ) {} |
| 37 | + |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + function checkStatus( xhr ) { |
| 42 | + // HTTP Status Codes: |
| 43 | + // 2xx : Success |
| 44 | + // 304 : Not Modified |
| 45 | + // 0 : Returned when running locally (file://) |
| 46 | + // 1223 : IE may change 204 to 1223 (see http://dev.jquery.com/ticket/1450) |
| 47 | + |
| 48 | + return ( xhr.readyState == 4 && ( ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status === 0 || xhr.status == 1223 ) ); |
| 49 | + } |
| 50 | + |
| 51 | + function getResponseText( xhr ) { |
| 52 | + if ( checkStatus( xhr ) ) |
| 53 | + return xhr.responseText; |
| 54 | + return null; |
| 55 | + } |
37 | 56 |
|
38 |
| - return null; |
39 |
| - }; |
| 57 | + var getResponseXml = function( xhr ) { |
| 58 | + if ( checkStatus( xhr ) ) { |
| 59 | + var xml = xhr.responseXML; |
| 60 | + return new CKEDITOR.xml( xml && xml.firstChild ? xml : xhr.responseText ); |
| 61 | + } |
| 62 | + return null; |
| 63 | + } |
40 | 64 |
|
41 |
| - var checkStatus = function( xhr ) { |
42 |
| - // HTTP Status Codes: |
43 |
| - // 2xx : Success |
44 |
| - // 304 : Not Modified |
45 |
| - // 0 : Returned when running locally (file://) |
46 |
| - // 1223 : IE may change 204 to 1223 (see http://dev.jquery.com/ticket/1450) |
| 65 | + function load( url, callback, getResponseFn ) { |
| 66 | + var async = !!callback; |
47 | 67 |
|
48 |
| - return ( xhr.readyState == 4 && ( ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status === 0 || xhr.status == 1223 ) ); |
49 |
| - }; |
| 68 | + var xhr = createXMLHttpRequest(); |
50 | 69 |
|
51 |
| - var getResponseText = function( xhr ) { |
52 |
| - if ( checkStatus( xhr ) ) |
53 |
| - return xhr.responseText; |
| 70 | + if ( !xhr ) |
54 | 71 | return null;
|
55 |
| - }; |
56 | 72 |
|
57 |
| - var getResponseXml = function( xhr ) { |
58 |
| - if ( checkStatus( xhr ) ) { |
59 |
| - var xml = xhr.responseXML; |
60 |
| - return new CKEDITOR.xml( xml && xml.firstChild ? xml : xhr.responseText ); |
61 |
| - } |
62 |
| - return null; |
63 |
| - }; |
| 73 | + xhr.open( 'GET', url, async ); |
64 | 74 |
|
65 |
| - var load = function( url, callback, getResponseFn ) { |
66 |
| - var async = !!callback; |
| 75 | + if ( async ) { |
| 76 | + // TODO: perform leak checks on this closure. |
| 77 | + xhr.onreadystatechange = function() { |
| 78 | + if ( xhr.readyState == 4 ) { |
| 79 | + callback( getResponseFn( xhr ) ); |
| 80 | + xhr = null; |
| 81 | + } |
| 82 | + }; |
| 83 | + } |
67 | 84 |
|
68 |
| - var xhr = createXMLHttpRequest(); |
| 85 | + xhr.send( null ); |
69 | 86 |
|
70 |
| - if ( !xhr ) |
71 |
| - return null; |
| 87 | + return async ? '' : getResponseFn( xhr ); |
| 88 | + } |
72 | 89 |
|
73 |
| - xhr.open( 'GET', url, async ); |
| 90 | + function post( url, data, contentType, callback, getResponseFn ) { |
| 91 | + var xhr = createXMLHttpRequest(); |
74 | 92 |
|
75 |
| - if ( async ) { |
76 |
| - // TODO: perform leak checks on this closure. |
77 |
| - xhr.onreadystatechange = function() { |
78 |
| - if ( xhr.readyState == 4 ) { |
79 |
| - callback( getResponseFn( xhr ) ); |
80 |
| - xhr = null; |
81 |
| - } |
82 |
| - }; |
83 |
| - } |
| 93 | + if ( !xhr ) |
| 94 | + return null; |
84 | 95 |
|
85 |
| - xhr.send( null ); |
| 96 | + xhr.open( 'POST', url, true ); |
86 | 97 |
|
87 |
| - return async ? '' : getResponseFn( xhr ); |
| 98 | + xhr.onreadystatechange = function() { |
| 99 | + if ( xhr.readyState == 4 ) { |
| 100 | + callback( getResponseFn( xhr ) ); |
| 101 | + xhr = null; |
| 102 | + } |
88 | 103 | };
|
89 | 104 |
|
| 105 | + xhr.setRequestHeader( 'Content-type', contentType || 'application/x-www-form-urlencoded; charset=UTF-8' ); |
| 106 | + |
| 107 | + xhr.send( data ); |
| 108 | + } |
| 109 | + |
90 | 110 | return {
|
91 | 111 | /**
|
92 | 112 | * Loads data from an URL as plain text.
|
|
111 | 131 | return load( url, callback, getResponseText );
|
112 | 132 | },
|
113 | 133 |
|
| 134 | + /** |
| 135 | + * Creates asynchronous POST `XMLHttpRequest` of given `url`, `data` and optional `contentType`. |
| 136 | + * Once the request is done, regardless if successful or not, `callback` is called |
| 137 | + * with `XMLHttpRequest#responseText` or `null` as an argument. |
| 138 | + * |
| 139 | + * CKEDITOR.ajax.post( 'url/post.php', 'foo=bar', null, function( data ) { |
| 140 | + * console.log( data ); |
| 141 | + * } ); |
| 142 | + * |
| 143 | + * CKEDITOR.ajax.post( 'url/post.php', JSON.stringify( { foo: 'bar' } ), 'application/json', function( data ) { |
| 144 | + * console.log( data ); |
| 145 | + * } ); |
| 146 | + * |
| 147 | + * @param {String} url URL of the request. |
| 148 | + * @param {String/Object/Array} data Data passed to `XMLHttpRequest#send`. |
| 149 | + * @param {String} [contentType='application/x-www-form-urlencoded; charset=UTF-8'] A value of `Content-type` header. |
| 150 | + * @param {Function} callback A callback executed asynchronously with `XMLHttpRequest#responseText` or `null` as an argument, |
| 151 | + * depending on `status` of the request. |
| 152 | + */ |
| 153 | + post: function( url, data, contentType, callback ) { |
| 154 | + return post( url, data, contentType, callback, getResponseText ); |
| 155 | + }, |
| 156 | + |
114 | 157 | /**
|
115 | 158 | * Loads data from an URL as XML.
|
116 | 159 | *
|
|
0 commit comments