Mercurial > hg > extraction-interface
comparison geotemco/lib/simile/ajax/scripts/xmlhttp.js @ 0:b12c99b7c3f0
commit for previous development
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Mon, 19 Jan 2015 17:13:49 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b12c99b7c3f0 |
---|---|
1 /** | |
2 * @fileOverview XmlHttp utility functions | |
3 * @name SimileAjax.XmlHttp | |
4 */ | |
5 | |
6 SimileAjax.XmlHttp = new Object(); | |
7 | |
8 /** | |
9 * Callback for XMLHttp onRequestStateChange. | |
10 */ | |
11 SimileAjax.XmlHttp._onReadyStateChange = function(xmlhttp, fError, fDone) { | |
12 switch (xmlhttp.readyState) { | |
13 // 1: Request not yet made | |
14 // 2: Contact established with server but nothing downloaded yet | |
15 // 3: Called multiple while downloading in progress | |
16 | |
17 // Download complete | |
18 case 4: | |
19 try { | |
20 if (xmlhttp.status == 0 // file:// urls, works on Firefox | |
21 || xmlhttp.status == 200 // http:// urls | |
22 ) { | |
23 if (fDone) { | |
24 fDone(xmlhttp); | |
25 } | |
26 } else { | |
27 if (fError) { | |
28 fError( | |
29 xmlhttp.statusText, | |
30 xmlhttp.status, | |
31 xmlhttp | |
32 ); | |
33 } | |
34 } | |
35 } catch (e) { | |
36 SimileAjax.Debug.exception("XmlHttp: Error handling onReadyStateChange", e); | |
37 } | |
38 break; | |
39 } | |
40 }; | |
41 | |
42 /** | |
43 * Creates an XMLHttpRequest object. On the first run, this | |
44 * function creates a platform-specific function for | |
45 * instantiating an XMLHttpRequest object and then replaces | |
46 * itself with that function. | |
47 */ | |
48 SimileAjax.XmlHttp._createRequest = function() { | |
49 if (SimileAjax.Platform.browser.isIE) { | |
50 var programIDs = [ | |
51 "Msxml2.XMLHTTP", | |
52 "Microsoft.XMLHTTP", | |
53 "Msxml2.XMLHTTP.4.0" | |
54 ]; | |
55 for (var i = 0; i < programIDs.length; i++) { | |
56 try { | |
57 var programID = programIDs[i]; | |
58 var f = function() { | |
59 return new ActiveXObject(programID); | |
60 }; | |
61 var o = f(); | |
62 | |
63 // We are replacing the SimileAjax._createXmlHttpRequest | |
64 // function with this inner function as we've | |
65 // found out that it works. This is so that we | |
66 // don't have to do all the testing over again | |
67 // on subsequent calls. | |
68 SimileAjax.XmlHttp._createRequest = f; | |
69 | |
70 return o; | |
71 } catch (e) { | |
72 // silent | |
73 } | |
74 } | |
75 // fall through to try new XMLHttpRequest(); | |
76 } | |
77 | |
78 try { | |
79 var f = function() { | |
80 return new XMLHttpRequest(); | |
81 }; | |
82 var o = f(); | |
83 | |
84 // We are replacing the SimileAjax._createXmlHttpRequest | |
85 // function with this inner function as we've | |
86 // found out that it works. This is so that we | |
87 // don't have to do all the testing over again | |
88 // on subsequent calls. | |
89 SimileAjax.XmlHttp._createRequest = f; | |
90 | |
91 return o; | |
92 } catch (e) { | |
93 throw new Error("Failed to create an XMLHttpRequest object"); | |
94 } | |
95 }; | |
96 | |
97 /** | |
98 * Performs an asynchronous HTTP GET. | |
99 * | |
100 * @param {Function} fError a function of the form | |
101 function(statusText, statusCode, xmlhttp) | |
102 * @param {Function} fDone a function of the form function(xmlhttp) | |
103 */ | |
104 SimileAjax.XmlHttp.get = function(url, fError, fDone) { | |
105 var xmlhttp = SimileAjax.XmlHttp._createRequest(); | |
106 | |
107 xmlhttp.open("GET", url, true); | |
108 xmlhttp.onreadystatechange = function() { | |
109 SimileAjax.XmlHttp._onReadyStateChange(xmlhttp, fError, fDone); | |
110 }; | |
111 xmlhttp.send(null); | |
112 }; | |
113 | |
114 /** | |
115 * Performs an asynchronous HTTP POST. | |
116 * | |
117 * @param {Function} fError a function of the form | |
118 function(statusText, statusCode, xmlhttp) | |
119 * @param {Function} fDone a function of the form function(xmlhttp) | |
120 */ | |
121 SimileAjax.XmlHttp.post = function(url, body, fError, fDone) { | |
122 var xmlhttp = SimileAjax.XmlHttp._createRequest(); | |
123 | |
124 xmlhttp.open("POST", url, true); | |
125 xmlhttp.onreadystatechange = function() { | |
126 SimileAjax.XmlHttp._onReadyStateChange(xmlhttp, fError, fDone); | |
127 }; | |
128 xmlhttp.send(body); | |
129 }; | |
130 | |
131 SimileAjax.XmlHttp._forceXML = function(xmlhttp) { | |
132 try { | |
133 xmlhttp.overrideMimeType("text/xml"); | |
134 } catch (e) { | |
135 xmlhttp.setrequestheader("Content-Type", "text/xml"); | |
136 } | |
137 }; |