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
* @requires OpenLayers/Request/XMLHttpRequest.js
9
//OpenLayers.ProxyHost = "";
10
OpenLayers.ProxyHost = "examples/proxy.cgi?url=";
13
* Ajax reader for OpenLayers
15
* @uri url to do remote XML http get
16
* @param {String} 'get' format params (x=y&a=b...)
17
* @who object to handle callbacks for this request
18
* @complete the function to be called on success
19
* @failure the function to be called on failure
21
* example usage from a caller:
23
* caps: function(request) {
27
* OpenLayers.loadURL(url,params,this,caps);
29
* Notice the above example does not provide an error handler; a default empty
30
* handler is provided which merely logs the error if a failure handler is not
37
* Function: OpenLayers.nullHandler
40
OpenLayers.nullHandler = function(request) {
41
OpenLayers.Console.userError(OpenLayers.i18n("unhandledRequest", {'statusText':request.statusText}));
45
* APIFunction: loadURL
46
* Background load a document. For more flexibility in using XMLHttpRequest,
47
* see the <OpenLayers.Request> methods.
50
* uri - {String} URI of source doc
51
* params - {String} or {Object} GET params. Either a string in the form
52
* "?hello=world&foo=bar" (do not forget the leading question mark)
53
* or an object in the form {'hello': 'world', 'foo': 'bar}
54
* caller - {Object} object which gets callbacks
55
* onComplete - {Function} Optional callback for success. The callback
56
* will be called with this set to caller and will receive the request
57
* object as an argument. Note that if you do not specify an onComplete
58
* function, <OpenLayers.nullHandler> will be called (which pops up a
59
* user friendly error message dialog).
60
* onFailure - {Function} Optional callback for failure. In the event of
61
* a failure, the callback will be called with this set to caller and will
62
* receive the request object as an argument. Note that if you do not
63
* specify an onComplete function, <OpenLayers.nullHandler> will be called
64
* (which pops up a user friendly error message dialog).
67
* {<OpenLayers.Request.XMLHttpRequest>} The request object. To abort loading,
68
* call request.abort().
70
OpenLayers.loadURL = function(uri, params, caller,
71
onComplete, onFailure) {
73
if(typeof params == 'string') {
74
params = OpenLayers.Util.getParameters(params);
76
var success = (onComplete) ? onComplete : OpenLayers.nullHandler;
77
var failure = (onFailure) ? onFailure : OpenLayers.nullHandler;
79
return OpenLayers.Request.GET({
80
url: uri, params: params,
81
success: success, failure: failure, scope: caller
86
* Function: parseXMLString
87
* Parse XML into a doc structure
93
* {?} Parsed AJAX Responsev
95
OpenLayers.parseXMLString = function(text) {
97
//MS sucks, if the server is bad it dies
98
var index = text.indexOf('<');
100
text = text.substring(index);
103
var ajaxResponse = OpenLayers.Util.Try(
105
var xmldom = new ActiveXObject('Microsoft.XMLDOM');
106
xmldom.loadXML(text);
110
return new DOMParser().parseFromString(text, 'text/xml');
113
var req = new XMLHttpRequest();
114
req.open("GET", "data:" + "text/xml" +
115
";charset=utf-8," + encodeURIComponent(text), false);
116
if (req.overrideMimeType) {
117
req.overrideMimeType("text/xml");
120
return req.responseXML;
129
* Namespace: OpenLayers.Ajax
134
* Method: emptyFunction
136
emptyFunction: function () {},
139
* Method: getTransport
142
* {Object} Transport mechanism for whichever browser we're in, or false if
145
getTransport: function() {
146
return OpenLayers.Util.Try(
147
function() {return new XMLHttpRequest();},
148
function() {return new ActiveXObject('Msxml2.XMLHTTP');},
149
function() {return new ActiveXObject('Microsoft.XMLHTTP');}
154
* Property: activeRequestCount
157
activeRequestCount: 0
161
* Namespace: OpenLayers.Ajax.Responders
164
OpenLayers.Ajax.Responders = {
167
* Property: responders
176
* responderToAdd - {?}
178
register: function(responderToAdd) {
179
for (var i = 0; i < this.responders.length; i++){
180
if (responderToAdd == this.responders[i]){
184
this.responders.push(responderToAdd);
191
* responderToRemove - {?}
193
unregister: function(responderToRemove) {
194
OpenLayers.Util.removeItem(this.reponders, responderToRemove);
205
dispatch: function(callback, request, transport) {
207
for (var i = 0; i < this.responders.length; i++) {
208
responder = this.responders[i];
210
if (responder[callback] &&
211
typeof responder[callback] == 'function') {
213
responder[callback].apply(responder,
214
[request, transport]);
221
OpenLayers.Ajax.Responders.register({
225
onCreate: function() {
226
OpenLayers.Ajax.activeRequestCount++;
230
* Function: onComplete
232
onComplete: function() {
233
OpenLayers.Ajax.activeRequestCount--;
238
* Class: OpenLayers.Ajax.Base
240
OpenLayers.Ajax.Base = OpenLayers.Class({
243
* Constructor: OpenLayers.Ajax.Base
248
initialize: function(options) {
252
contentType: 'application/xml',
255
OpenLayers.Util.extend(this.options, options || {});
257
this.options.method = this.options.method.toLowerCase();
259
if (typeof this.options.parameters == 'string') {
260
this.options.parameters =
261
OpenLayers.Util.getParameters(this.options.parameters);
267
* Class: OpenLayers.Ajax.Request
268
* *Deprecated*. Use <OpenLayers.Request> method instead.
271
* - <OpenLayers.Ajax.Base>
273
OpenLayers.Ajax.Request = OpenLayers.Class(OpenLayers.Ajax.Base, {
276
* Property: _complete
283
* Constructor: OpenLayers.Ajax.Request
289
initialize: function(url, options) {
290
OpenLayers.Ajax.Base.prototype.initialize.apply(this, [options]);
292
if (OpenLayers.ProxyHost && OpenLayers.String.startsWith(url, "http")) {
293
url = OpenLayers.ProxyHost + encodeURIComponent(url);
296
this.transport = OpenLayers.Ajax.getTransport();
306
request: function(url) {
308
this.method = this.options.method;
309
var params = OpenLayers.Util.extend({}, this.options.parameters);
311
if (this.method != 'get' && this.method != 'post') {
312
// simulate other verbs over post
313
params['_method'] = this.method;
314
this.method = 'post';
317
this.parameters = params;
319
if (params = OpenLayers.Util.getParameterString(params)) {
320
// when GET, append parameters to URL
321
if (this.method == 'get') {
322
this.url += ((this.url.indexOf('?') > -1) ? '&' : '?') + params;
323
} else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) {
328
var response = new OpenLayers.Ajax.Response(this);
329
if (this.options.onCreate) {
330
this.options.onCreate(response);
333
OpenLayers.Ajax.Responders.dispatch('onCreate',
337
this.transport.open(this.method.toUpperCase(),
339
this.options.asynchronous);
341
if (this.options.asynchronous) {
343
OpenLayers.Function.bind(this.respondToReadyState, this, 1),
347
this.transport.onreadystatechange =
348
OpenLayers.Function.bind(this.onStateChange, this);
349
this.setRequestHeaders();
351
this.body = this.method == 'post' ?
352
(this.options.postBody || params) : null;
353
this.transport.send(this.body);
355
// Force Firefox to handle ready state 4 for synchronous requests
356
if (!this.options.asynchronous &&
357
this.transport.overrideMimeType) {
358
this.onStateChange();
361
this.dispatchException(e);
366
* Method: onStateChange
368
onStateChange: function() {
369
var readyState = this.transport.readyState;
370
if (readyState > 1 && !((readyState == 4) && this._complete)) {
371
this.respondToReadyState(this.transport.readyState);
376
* Method: setRequestHeaders
378
setRequestHeaders: function() {
380
'X-Requested-With': 'XMLHttpRequest',
381
'Accept': 'text/javascript, text/html, application/xml, text/xml, */*',
385
if (this.method == 'post') {
386
headers['Content-type'] = this.options.contentType +
387
(this.options.encoding ? '; charset=' + this.options.encoding : '');
389
/* Force "Connection: close" for older Mozilla browsers to work
390
* around a bug where XMLHttpRequest sends an incorrect
391
* Content-length header. See Mozilla Bugzilla #246651.
393
if (this.transport.overrideMimeType &&
394
(navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) {
395
headers['Connection'] = 'close';
398
// user-defined headers
399
if (typeof this.options.requestHeaders == 'object') {
400
var extras = this.options.requestHeaders;
402
if (typeof extras.push == 'function') {
403
for (var i = 0, length = extras.length; i < length; i += 2) {
404
headers[extras[i]] = extras[i+1];
407
for (var i in extras) {
408
headers[i] = extras[i];
413
for (var name in headers) {
414
this.transport.setRequestHeader(name, headers[name]);
424
success: function() {
425
var status = this.getStatus();
426
return !status || (status >=200 && status < 300);
435
getStatus: function() {
437
return this.transport.status || 0;
444
* Method: respondToReadyState
449
respondToReadyState: function(readyState) {
450
var state = OpenLayers.Ajax.Request.Events[readyState];
451
var response = new OpenLayers.Ajax.Response(this);
453
if (state == 'Complete') {
455
this._complete = true;
456
(this.options['on' + response.status] ||
457
this.options['on' + (this.success() ? 'Success' : 'Failure')] ||
458
OpenLayers.Ajax.emptyFunction)(response);
460
this.dispatchException(e);
463
var contentType = response.getHeader('Content-type');
467
(this.options['on' + state] ||
468
OpenLayers.Ajax.emptyFunction)(response);
469
OpenLayers.Ajax.Responders.dispatch('on' + state,
473
this.dispatchException(e);
476
if (state == 'Complete') {
477
// avoid memory leak in MSIE: clean up
478
this.transport.onreadystatechange = OpenLayers.Ajax.emptyFunction;
486
* name - {String} Header name
489
* {?} - response header for the given name
491
getHeader: function(name) {
493
return this.transport.getResponseHeader(name);
500
* Method: dispatchException
501
* If the optional onException function is set, execute it
502
* and then dispatch the call to any other listener registered
505
* If no optional onException function is set, we suspect that
506
* the user may have also not used
507
* OpenLayers.Ajax.Responders.register to register a listener
508
* for the onException call. To make sure that something
509
* gets done with this exception, only dispatch the call if there
512
* If you explicitly want to swallow exceptions, set
513
* request.options.onException to an empty function (function(){})
514
* or register an empty function with <OpenLayers.Ajax.Responders>
520
dispatchException: function(exception) {
521
var handler = this.options.onException;
523
// call options.onException and alert any other listeners
524
handler(this, exception);
525
OpenLayers.Ajax.Responders.dispatch('onException', this, exception);
527
// check if there are any other listeners
528
var listener = false;
529
var responders = OpenLayers.Ajax.Responders.responders;
530
for (var i = 0; i < responders.length; i++) {
531
if(responders[i].onException) {
537
// call all listeners
538
OpenLayers.Ajax.Responders.dispatch('onException', this, exception);
540
// let the exception through
551
OpenLayers.Ajax.Request.Events =
552
['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
555
* Class: OpenLayers.Ajax.Response
557
OpenLayers.Ajax.Response = OpenLayers.Class({
568
* Property: statusText
575
* Constructor: OpenLayers.Ajax.Response
580
initialize: function(request) {
581
this.request = request;
582
var transport = this.transport = request.transport,
583
readyState = this.readyState = transport.readyState;
585
if ((readyState > 2 &&
586
!(!!(window.attachEvent && !window.opera))) ||
588
this.status = this.getStatus();
589
this.statusText = this.getStatusText();
590
this.responseText = transport.responseText == null ?
591
'' : String(transport.responseText);
594
if(readyState == 4) {
595
var xml = transport.responseXML;
596
this.responseXML = xml === undefined ? null : xml;
603
getStatus: OpenLayers.Ajax.Request.prototype.getStatus,
606
* Method: getStatustext
609
* {String} - statusText
611
getStatusText: function() {
613
return this.transport.statusText || '';
622
getHeader: OpenLayers.Ajax.Request.prototype.getHeader,
625
* Method: getResponseHeader
628
* {?} - response header for given name
630
getResponseHeader: function(name) {
631
return this.transport.getResponseHeader(name);
637
* Function: getElementsByTagNameNS
648
OpenLayers.Ajax.getElementsByTagNameNS = function(parentnode, nsuri,
651
if (parentnode.getElementsByTagNameNS) {
652
elem = parentnode.getElementsByTagNameNS(nsuri, tagname);
654
elem = parentnode.getElementsByTagName(nsprefix + ':' + tagname);
661
* Function: serializeXMLToString
662
* Wrapper function around XMLSerializer, which doesn't exist/work in
663
* IE/Safari. We need to come up with a way to serialize in those browser:
664
* for now, these browsers will just fail. #535, #536
667
* xmldom {XMLNode} xml dom to serialize
672
OpenLayers.Ajax.serializeXMLToString = function(xmldom) {
673
var serializer = new XMLSerializer();
674
var data = serializer.serializeToString(xmldom);