1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3
* full text of the license. */
6
* Namespace: OpenLayers.Request
7
* The OpenLayers.Request namespace contains convenience methods for working
8
* with XMLHttpRequests. These methods work with a cross-browser
9
* W3C compliant <OpenLayers.Request.XMLHttpRequest> class.
11
OpenLayers.Request = {
14
* Constant: DEFAULT_CONFIG
15
* {Object} Default configuration for all requests.
19
url: window.location.href,
24
proxy: OpenLayers.ProxyHost,
27
callback: function() {},
35
* Create a new XMLHttpRequest object, open it, set any headers, bind
36
* a callback to done state, and send any data. It is recommended that
37
* you use one <GET>, <POST>, <PUT>, <DELETE>, <OPTIONS>, or <HEAD>.
38
* This method is only documented to provide detail on the configuration
39
* options available to all request methods.
42
* config - {Object} Object containing properties for configuring the
43
* request. Allowed configuration properties are described below.
44
* This object is modified and should not be reused.
46
* Allowed config properties:
47
* method - {String} One of GET, POST, PUT, DELETE, HEAD, or
48
* OPTIONS. Default is GET.
49
* url - {String} URL for the request.
50
* async - {Boolean} Open an asynchronous request. Default is true.
51
* user - {String} User for relevant authentication scheme. Set
52
* to null to clear current user.
53
* password - {String} Password for relevant authentication scheme.
54
* Set to null to clear current password.
55
* proxy - {String} Optional proxy. Defaults to
56
* <OpenLayers.ProxyHost>.
57
* params - {Object} Any key:value pairs to be appended to the
58
* url as a query string. Assumes url doesn't already include a query
59
* string or hash. Typically, this is only appropriate for <GET>
60
* requests where the query string will be appended to the url.
61
* Parameter values that are arrays will be
62
* concatenated with a comma (note that this goes against form-encoding)
63
* as is done with <OpenLayers.Util.getParameterString>.
64
* headers - {Object} Object with header:value pairs to be set on
66
* data - {String | Document} Optional data to send with the request.
67
* Typically, this is only used with <POST> and <PUT> requests.
68
* Make sure to provide the appropriate "Content-Type" header for your
69
* data. For <POST> and <PUT> requests, the content type defaults to
70
* "application-xml". If your data is a different content type, or
71
* if you are using a different HTTP method, set the "Content-Type"
72
* header to match your data type.
73
* callback - {Function} Function to call when request is done.
74
* To determine if the request failed, check request.status (200
76
* success - {Function} Optional function to call if request status is in
77
* the 200s. This will be called in addition to callback above and
78
* would typically only be used as an alternative.
79
* failure - {Function} Optional function to call if request status is not
80
* in the 200s. This will be called in addition to callback above and
81
* would typically only be used as an alternative.
82
* scope - {Object} If callback is a public method on some object,
83
* set the scope to that object.
86
* {XMLHttpRequest} Request object. To abort the request before a response
87
* is received, call abort() on the request object.
89
issue: function(config) {
90
// apply default config - proxy host may have changed
91
var defaultConfig = OpenLayers.Util.extend(
93
{proxy: OpenLayers.ProxyHost}
95
config = OpenLayers.Util.applyDefaults(config, defaultConfig);
97
// create request, open, and set headers
98
var request = new OpenLayers.Request.XMLHttpRequest();
101
var paramString = OpenLayers.Util.getParameterString(config.params);
102
if(paramString.length > 0) {
103
var separator = (url.indexOf('?') > -1) ? '&' : '?';
104
url += separator + paramString;
107
if(config.proxy && (url.indexOf("http") == 0)) {
108
url = config.proxy + encodeURIComponent(url);
111
config.method, url, config.async, config.user, config.password
113
for(var header in config.headers) {
114
request.setRequestHeader(header, config.headers[header]);
117
// bind callbacks to readyState 4 (done)
118
var complete = (config.scope) ?
119
OpenLayers.Function.bind(config.callback, config.scope) :
122
// optional success callback
125
success = (config.scope) ?
126
OpenLayers.Function.bind(config.success, config.scope) :
130
// optional failure callback
133
failure = (config.scope) ?
134
OpenLayers.Function.bind(config.failure, config.scope) :
138
request.onreadystatechange = function() {
139
if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) {
141
if(success && (!request.status ||
142
(request.status >= 200 && request.status < 300))) {
145
if(failure && (request.status &&
146
(request.status < 200 || request.status >= 300))) {
152
// send request (optionally with data) and return
153
request.send(config.data);
159
* Send an HTTP GET request. Additional configuration properties are
160
* documented in the <issue> method, with the method property set
164
* config - {Object} Object with properties for configuring the request.
165
* See the <issue> method for documentation of allowed properties.
166
* This object is modified and should not be reused.
169
* {XMLHttpRequest} Request object.
171
GET: function(config) {
172
config = OpenLayers.Util.extend(config, {method: "GET"});
173
return OpenLayers.Request.issue(config);
178
* Send a POST request. Additional configuration properties are
179
* documented in the <issue> method, with the method property set
180
* to POST and "Content-Type" header set to "application/xml".
183
* config - {Object} Object with properties for configuring the request.
184
* See the <issue> method for documentation of allowed properties. The
185
* default "Content-Type" header will be set to "application-xml" if
186
* none is provided. This object is modified and should not be reused.
189
* {XMLHttpRequest} Request object.
191
POST: function(config) {
192
config = OpenLayers.Util.extend(config, {method: "POST"});
193
// set content type to application/xml if it isn't already set
194
config.headers = config.headers ? config.headers : {};
195
if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) {
196
config.headers["Content-Type"] = "application/xml";
198
return OpenLayers.Request.issue(config);
203
* Send an HTTP PUT request. Additional configuration properties are
204
* documented in the <issue> method, with the method property set
205
* to PUT and "Content-Type" header set to "application/xml".
208
* config - {Object} Object with properties for configuring the request.
209
* See the <issue> method for documentation of allowed properties. The
210
* default "Content-Type" header will be set to "application-xml" if
211
* none is provided. This object is modified and should not be reused.
214
* {XMLHttpRequest} Request object.
216
PUT: function(config) {
217
config = OpenLayers.Util.extend(config, {method: "PUT"});
218
// set content type to application/xml if it isn't already set
219
config.headers = config.headers ? config.headers : {};
220
if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) {
221
config.headers["Content-Type"] = "application/xml";
223
return OpenLayers.Request.issue(config);
228
* Send an HTTP DELETE request. Additional configuration properties are
229
* documented in the <issue> method, with the method property set
233
* config - {Object} Object with properties for configuring the request.
234
* See the <issue> method for documentation of allowed properties.
235
* This object is modified and should not be reused.
238
* {XMLHttpRequest} Request object.
240
DELETE: function(config) {
241
config = OpenLayers.Util.extend(config, {method: "DELETE"});
242
return OpenLayers.Request.issue(config);
247
* Send an HTTP HEAD request. Additional configuration properties are
248
* documented in the <issue> method, with the method property set
252
* config - {Object} Object with properties for configuring the request.
253
* See the <issue> method for documentation of allowed properties.
254
* This object is modified and should not be reused.
257
* {XMLHttpRequest} Request object.
259
HEAD: function(config) {
260
config = OpenLayers.Util.extend(config, {method: "HEAD"});
261
return OpenLayers.Request.issue(config);
266
* Send an HTTP OPTIONS request. Additional configuration properties are
267
* documented in the <issue> method, with the method property set
271
* config - {Object} Object with properties for configuring the request.
272
* See the <issue> method for documentation of allowed properties.
273
* This object is modified and should not be reused.
276
* {XMLHttpRequest} Request object.
278
OPTIONS: function(config) {
279
config = OpenLayers.Util.extend(config, {method: "OPTIONS"});
280
return OpenLayers.Request.issue(config);