1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Events.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Namespace: OpenLayers.Request |
---|
12 | * The OpenLayers.Request namespace contains convenience methods for working |
---|
13 | * with XMLHttpRequests. These methods work with a cross-browser |
---|
14 | * W3C compliant <OpenLayers.Request.XMLHttpRequest> class. |
---|
15 | */ |
---|
16 | OpenLayers.Request = { |
---|
17 | |
---|
18 | /** |
---|
19 | * Constant: DEFAULT_CONFIG |
---|
20 | * {Object} Default configuration for all requests. |
---|
21 | */ |
---|
22 | DEFAULT_CONFIG: { |
---|
23 | method: "GET", |
---|
24 | url: window.location.href, |
---|
25 | async: true, |
---|
26 | user: undefined, |
---|
27 | password: undefined, |
---|
28 | params: null, |
---|
29 | proxy: OpenLayers.ProxyHost, |
---|
30 | headers: {}, |
---|
31 | data: null, |
---|
32 | callback: function() {}, |
---|
33 | success: null, |
---|
34 | failure: null, |
---|
35 | scope: null |
---|
36 | }, |
---|
37 | |
---|
38 | /** |
---|
39 | * APIProperty: events |
---|
40 | * {<OpenLayers.Events>} An events object that handles all |
---|
41 | * events on the {<OpenLayers.Request>} object. |
---|
42 | * |
---|
43 | * All event listeners will receive an event object with three properties: |
---|
44 | * request - {<OpenLayers.Request.XMLHttpRequest>} The request object. |
---|
45 | * config - {Object} The config object sent to the specific request method. |
---|
46 | * requestUrl - {String} The request url. |
---|
47 | * |
---|
48 | * Supported event types: |
---|
49 | * complete - Triggered when we have a response from the request, if a |
---|
50 | * listener returns false, no further response processing will take |
---|
51 | * place. |
---|
52 | * success - Triggered when the HTTP response has a success code (200-299). |
---|
53 | * failure - Triggered when the HTTP response does not have a success code. |
---|
54 | */ |
---|
55 | events: new OpenLayers.Events(this, null, ["complete", "success", "failure"]), |
---|
56 | |
---|
57 | /** |
---|
58 | * APIMethod: issue |
---|
59 | * Create a new XMLHttpRequest object, open it, set any headers, bind |
---|
60 | * a callback to done state, and send any data. It is recommended that |
---|
61 | * you use one <GET>, <POST>, <PUT>, <DELETE>, <OPTIONS>, or <HEAD>. |
---|
62 | * This method is only documented to provide detail on the configuration |
---|
63 | * options available to all request methods. |
---|
64 | * |
---|
65 | * Parameters: |
---|
66 | * config - {Object} Object containing properties for configuring the |
---|
67 | * request. Allowed configuration properties are described below. |
---|
68 | * This object is modified and should not be reused. |
---|
69 | * |
---|
70 | * Allowed config properties: |
---|
71 | * method - {String} One of GET, POST, PUT, DELETE, HEAD, or |
---|
72 | * OPTIONS. Default is GET. |
---|
73 | * url - {String} URL for the request. |
---|
74 | * async - {Boolean} Open an asynchronous request. Default is true. |
---|
75 | * user - {String} User for relevant authentication scheme. Set |
---|
76 | * to null to clear current user. |
---|
77 | * password - {String} Password for relevant authentication scheme. |
---|
78 | * Set to null to clear current password. |
---|
79 | * proxy - {String} Optional proxy. Defaults to |
---|
80 | * <OpenLayers.ProxyHost>. |
---|
81 | * params - {Object} Any key:value pairs to be appended to the |
---|
82 | * url as a query string. Assumes url doesn't already include a query |
---|
83 | * string or hash. Typically, this is only appropriate for <GET> |
---|
84 | * requests where the query string will be appended to the url. |
---|
85 | * Parameter values that are arrays will be |
---|
86 | * concatenated with a comma (note that this goes against form-encoding) |
---|
87 | * as is done with <OpenLayers.Util.getParameterString>. |
---|
88 | * headers - {Object} Object with header:value pairs to be set on |
---|
89 | * the request. |
---|
90 | * data - {String | Document} Optional data to send with the request. |
---|
91 | * Typically, this is only used with <POST> and <PUT> requests. |
---|
92 | * Make sure to provide the appropriate "Content-Type" header for your |
---|
93 | * data. For <POST> and <PUT> requests, the content type defaults to |
---|
94 | * "application-xml". If your data is a different content type, or |
---|
95 | * if you are using a different HTTP method, set the "Content-Type" |
---|
96 | * header to match your data type. |
---|
97 | * callback - {Function} Function to call when request is done. |
---|
98 | * To determine if the request failed, check request.status (200 |
---|
99 | * indicates success). |
---|
100 | * success - {Function} Optional function to call if request status is in |
---|
101 | * the 200s. This will be called in addition to callback above and |
---|
102 | * would typically only be used as an alternative. |
---|
103 | * failure - {Function} Optional function to call if request status is not |
---|
104 | * in the 200s. This will be called in addition to callback above and |
---|
105 | * would typically only be used as an alternative. |
---|
106 | * scope - {Object} If callback is a public method on some object, |
---|
107 | * set the scope to that object. |
---|
108 | * |
---|
109 | * Returns: |
---|
110 | * {XMLHttpRequest} Request object. To abort the request before a response |
---|
111 | * is received, call abort() on the request object. |
---|
112 | */ |
---|
113 | issue: function(config) { |
---|
114 | // apply default config - proxy host may have changed |
---|
115 | var defaultConfig = OpenLayers.Util.extend( |
---|
116 | this.DEFAULT_CONFIG, |
---|
117 | {proxy: OpenLayers.ProxyHost} |
---|
118 | ); |
---|
119 | config = OpenLayers.Util.applyDefaults(config, defaultConfig); |
---|
120 | |
---|
121 | // create request, open, and set headers |
---|
122 | var request = new OpenLayers.Request.XMLHttpRequest(); |
---|
123 | var url = config.url; |
---|
124 | if(config.params) { |
---|
125 | var paramString = OpenLayers.Util.getParameterString(config.params); |
---|
126 | if(paramString.length > 0) { |
---|
127 | var separator = (url.indexOf('?') > -1) ? '&' : '?'; |
---|
128 | url += separator + paramString; |
---|
129 | } |
---|
130 | } |
---|
131 | if(config.proxy && (url.indexOf("http") == 0)) { |
---|
132 | if(typeof config.proxy == "function") { |
---|
133 | url = config.proxy(url); |
---|
134 | } else { |
---|
135 | url = config.proxy + encodeURIComponent(url); |
---|
136 | } |
---|
137 | } |
---|
138 | request.open( |
---|
139 | config.method, url, config.async, config.user, config.password |
---|
140 | ); |
---|
141 | for(var header in config.headers) { |
---|
142 | request.setRequestHeader(header, config.headers[header]); |
---|
143 | } |
---|
144 | |
---|
145 | var events = this.events; |
---|
146 | |
---|
147 | // we want to execute runCallbacks with "this" as the |
---|
148 | // execution scope |
---|
149 | var self = this; |
---|
150 | |
---|
151 | request.onreadystatechange = function() { |
---|
152 | if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) { |
---|
153 | var proceed = events.triggerEvent( |
---|
154 | "complete", |
---|
155 | {request: request, config: config, requestUrl: url} |
---|
156 | ); |
---|
157 | if(proceed !== false) { |
---|
158 | self.runCallbacks( |
---|
159 | {request: request, config: config, requestUrl: url} |
---|
160 | ); |
---|
161 | } |
---|
162 | } |
---|
163 | }; |
---|
164 | |
---|
165 | // send request (optionally with data) and return |
---|
166 | // call in a timeout for asynchronous requests so the return is |
---|
167 | // available before readyState == 4 for cached docs |
---|
168 | if(config.async === false) { |
---|
169 | request.send(config.data); |
---|
170 | } else { |
---|
171 | window.setTimeout(function(){ |
---|
172 | if (request._aborted !== true) { |
---|
173 | request.send(config.data); |
---|
174 | } |
---|
175 | }, 0); |
---|
176 | } |
---|
177 | return request; |
---|
178 | }, |
---|
179 | |
---|
180 | /** |
---|
181 | * Method: runCallbacks |
---|
182 | * Calls the complete, success and failure callbacks. Application |
---|
183 | * can listen to the "complete" event, have the listener |
---|
184 | * display a confirm window and always return false, and |
---|
185 | * execute OpenLayers.Request.runCallbacks if the user |
---|
186 | * hits "yes" in the confirm window. |
---|
187 | * |
---|
188 | * Parameters: |
---|
189 | * options - {Object} Hash containing request, config and requestUrl keys |
---|
190 | */ |
---|
191 | runCallbacks: function(options) { |
---|
192 | var request = options.request; |
---|
193 | var config = options.config; |
---|
194 | |
---|
195 | // bind callbacks to readyState 4 (done) |
---|
196 | var complete = (config.scope) ? |
---|
197 | OpenLayers.Function.bind(config.callback, config.scope) : |
---|
198 | config.callback; |
---|
199 | |
---|
200 | // optional success callback |
---|
201 | var success; |
---|
202 | if(config.success) { |
---|
203 | success = (config.scope) ? |
---|
204 | OpenLayers.Function.bind(config.success, config.scope) : |
---|
205 | config.success; |
---|
206 | } |
---|
207 | |
---|
208 | // optional failure callback |
---|
209 | var failure; |
---|
210 | if(config.failure) { |
---|
211 | failure = (config.scope) ? |
---|
212 | OpenLayers.Function.bind(config.failure, config.scope) : |
---|
213 | config.failure; |
---|
214 | } |
---|
215 | |
---|
216 | complete(request); |
---|
217 | |
---|
218 | if (!request.status || (request.status >= 200 && request.status < 300)) { |
---|
219 | this.events.triggerEvent("success", options); |
---|
220 | if(success) { |
---|
221 | success(request); |
---|
222 | } |
---|
223 | } |
---|
224 | if(request.status && (request.status < 200 || request.status >= 300)) { |
---|
225 | this.events.triggerEvent("failure", options); |
---|
226 | if(failure) { |
---|
227 | failure(request); |
---|
228 | } |
---|
229 | } |
---|
230 | }, |
---|
231 | |
---|
232 | /** |
---|
233 | * APIMethod: GET |
---|
234 | * Send an HTTP GET request. Additional configuration properties are |
---|
235 | * documented in the <issue> method, with the method property set |
---|
236 | * to GET. |
---|
237 | * |
---|
238 | * Parameters: |
---|
239 | * config - {Object} Object with properties for configuring the request. |
---|
240 | * See the <issue> method for documentation of allowed properties. |
---|
241 | * This object is modified and should not be reused. |
---|
242 | * |
---|
243 | * Returns: |
---|
244 | * {XMLHttpRequest} Request object. |
---|
245 | */ |
---|
246 | GET: function(config) { |
---|
247 | config = OpenLayers.Util.extend(config, {method: "GET"}); |
---|
248 | return OpenLayers.Request.issue(config); |
---|
249 | }, |
---|
250 | |
---|
251 | /** |
---|
252 | * APIMethod: POST |
---|
253 | * Send a POST request. Additional configuration properties are |
---|
254 | * documented in the <issue> method, with the method property set |
---|
255 | * to POST and "Content-Type" header set to "application/xml". |
---|
256 | * |
---|
257 | * Parameters: |
---|
258 | * config - {Object} Object with properties for configuring the request. |
---|
259 | * See the <issue> method for documentation of allowed properties. The |
---|
260 | * default "Content-Type" header will be set to "application-xml" if |
---|
261 | * none is provided. This object is modified and should not be reused. |
---|
262 | * |
---|
263 | * Returns: |
---|
264 | * {XMLHttpRequest} Request object. |
---|
265 | */ |
---|
266 | POST: function(config) { |
---|
267 | config = OpenLayers.Util.extend(config, {method: "POST"}); |
---|
268 | // set content type to application/xml if it isn't already set |
---|
269 | config.headers = config.headers ? config.headers : {}; |
---|
270 | if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) { |
---|
271 | config.headers["Content-Type"] = "application/xml"; |
---|
272 | } |
---|
273 | return OpenLayers.Request.issue(config); |
---|
274 | }, |
---|
275 | |
---|
276 | /** |
---|
277 | * APIMethod: PUT |
---|
278 | * Send an HTTP PUT request. Additional configuration properties are |
---|
279 | * documented in the <issue> method, with the method property set |
---|
280 | * to PUT and "Content-Type" header set to "application/xml". |
---|
281 | * |
---|
282 | * Parameters: |
---|
283 | * config - {Object} Object with properties for configuring the request. |
---|
284 | * See the <issue> method for documentation of allowed properties. The |
---|
285 | * default "Content-Type" header will be set to "application-xml" if |
---|
286 | * none is provided. This object is modified and should not be reused. |
---|
287 | * |
---|
288 | * Returns: |
---|
289 | * {XMLHttpRequest} Request object. |
---|
290 | */ |
---|
291 | PUT: function(config) { |
---|
292 | config = OpenLayers.Util.extend(config, {method: "PUT"}); |
---|
293 | // set content type to application/xml if it isn't already set |
---|
294 | config.headers = config.headers ? config.headers : {}; |
---|
295 | if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) { |
---|
296 | config.headers["Content-Type"] = "application/xml"; |
---|
297 | } |
---|
298 | return OpenLayers.Request.issue(config); |
---|
299 | }, |
---|
300 | |
---|
301 | /** |
---|
302 | * APIMethod: DELETE |
---|
303 | * Send an HTTP DELETE request. Additional configuration properties are |
---|
304 | * documented in the <issue> method, with the method property set |
---|
305 | * to DELETE. |
---|
306 | * |
---|
307 | * Parameters: |
---|
308 | * config - {Object} Object with properties for configuring the request. |
---|
309 | * See the <issue> method for documentation of allowed properties. |
---|
310 | * This object is modified and should not be reused. |
---|
311 | * |
---|
312 | * Returns: |
---|
313 | * {XMLHttpRequest} Request object. |
---|
314 | */ |
---|
315 | DELETE: function(config) { |
---|
316 | config = OpenLayers.Util.extend(config, {method: "DELETE"}); |
---|
317 | return OpenLayers.Request.issue(config); |
---|
318 | }, |
---|
319 | |
---|
320 | /** |
---|
321 | * APIMethod: HEAD |
---|
322 | * Send an HTTP HEAD request. Additional configuration properties are |
---|
323 | * documented in the <issue> method, with the method property set |
---|
324 | * to HEAD. |
---|
325 | * |
---|
326 | * Parameters: |
---|
327 | * config - {Object} Object with properties for configuring the request. |
---|
328 | * See the <issue> method for documentation of allowed properties. |
---|
329 | * This object is modified and should not be reused. |
---|
330 | * |
---|
331 | * Returns: |
---|
332 | * {XMLHttpRequest} Request object. |
---|
333 | */ |
---|
334 | HEAD: function(config) { |
---|
335 | config = OpenLayers.Util.extend(config, {method: "HEAD"}); |
---|
336 | return OpenLayers.Request.issue(config); |
---|
337 | }, |
---|
338 | |
---|
339 | /** |
---|
340 | * APIMethod: OPTIONS |
---|
341 | * Send an HTTP OPTIONS request. Additional configuration properties are |
---|
342 | * documented in the <issue> method, with the method property set |
---|
343 | * to OPTIONS. |
---|
344 | * |
---|
345 | * Parameters: |
---|
346 | * config - {Object} Object with properties for configuring the request. |
---|
347 | * See the <issue> method for documentation of allowed properties. |
---|
348 | * This object is modified and should not be reused. |
---|
349 | * |
---|
350 | * Returns: |
---|
351 | * {XMLHttpRequest} Request object. |
---|
352 | */ |
---|
353 | OPTIONS: function(config) { |
---|
354 | config = OpenLayers.Util.extend(config, {method: "OPTIONS"}); |
---|
355 | return OpenLayers.Request.issue(config); |
---|
356 | } |
---|
357 | |
---|
358 | }; |
---|