Adding cross-browser XMLHttpRequest functionality and convenience methods around it in the OpenLayers.Request namespace. Deprecating OpenLayers.Ajax.Request. Full support sync/async requests using all HTTP verbs now. r=elemoine (closes #1565)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@7335 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -30,16 +30,18 @@ OpenLayers.ProxyHost = "";
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @param {} request
|
||||
*/
|
||||
/**
|
||||
* Function: OpenLayers.nullHandler
|
||||
* @param {} request
|
||||
*/
|
||||
OpenLayers.nullHandler = function(request) {
|
||||
alert(OpenLayers.i18n("unhandledRequest", {'statusText':request.statusText}));
|
||||
};
|
||||
|
||||
/**
|
||||
* Function: loadURL
|
||||
* Background load a document.
|
||||
* Background load a document. For more flexibility in using XMLHttpRequest,
|
||||
* see the <OpenLayers.Request> methods.
|
||||
*
|
||||
* Parameters:
|
||||
* uri - {String} URI of source doc
|
||||
@@ -49,35 +51,32 @@ OpenLayers.nullHandler = function(request) {
|
||||
* caller - {Object} object which gets callbacks
|
||||
* onComplete - {Function} Optional callback for success. The callback
|
||||
* will be called with this set to caller and will receive the request
|
||||
* object as an argument.
|
||||
* object as an argument. Note that if you do not specify an onComplete
|
||||
* function, <OpenLayers.nullHandler> will be called (which pops up an
|
||||
* alert dialog).
|
||||
* onFailure - {Function} Optional callback for failure. In the event of
|
||||
* a failure, the callback will be called with this set to caller and will
|
||||
* receive the request object as an argument.
|
||||
* receive the request object as an argument. Note that if you do not
|
||||
* specify an onComplete function, <OpenLayers.nullHandler> will be called
|
||||
* (which pops up an alert dialog).
|
||||
*
|
||||
* Returns:
|
||||
* {XMLHttpRequest} The request object. To abort loading, call
|
||||
* request.abort().
|
||||
* {<OpenLayers.Request.XMLHttpRequest>} The request object. To abort loading,
|
||||
* call request.abort().
|
||||
*/
|
||||
OpenLayers.loadURL = function(uri, params, caller,
|
||||
onComplete, onFailure) {
|
||||
|
||||
var success = (onComplete) ? OpenLayers.Function.bind(onComplete, caller)
|
||||
: OpenLayers.nullHandler;
|
||||
|
||||
var failure = (onFailure) ? OpenLayers.Function.bind(onFailure, caller)
|
||||
: OpenLayers.nullHandler;
|
||||
|
||||
// from prototype.js
|
||||
var request = new OpenLayers.Ajax.Request(
|
||||
uri,
|
||||
{
|
||||
method: 'get',
|
||||
parameters: params,
|
||||
onComplete: success,
|
||||
onFailure: failure
|
||||
}
|
||||
);
|
||||
return request.transport;
|
||||
|
||||
if(typeof params == 'string') {
|
||||
params = OpenLayers.Util.getParameters(params);
|
||||
}
|
||||
var success = (onComplete) ? onComplete : OpenLayers.nullHandler;
|
||||
var failure = (onFailure) ? onFailure : OpenLayers.nullHandler;
|
||||
|
||||
return OpenLayers.Request.GET({
|
||||
url: uri, params: params,
|
||||
success: success, failure: failure, scope: caller
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -263,6 +262,7 @@ OpenLayers.Ajax.Base = OpenLayers.Class({
|
||||
|
||||
/**
|
||||
* Class: OpenLayers.Ajax.Request
|
||||
* *Deprecated*. Use <OpenLayers.Request> method instead.
|
||||
*
|
||||
* Inherit:
|
||||
* - <OpenLayers.Ajax.Base>
|
||||
|
||||
@@ -249,11 +249,9 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
|
||||
*
|
||||
*/
|
||||
fetchLink: function(href) {
|
||||
var request = new OpenLayers.Ajax.Request(href,
|
||||
{method: 'get', asynchronous: false });
|
||||
|
||||
if (request && request.transport) {
|
||||
return request.transport.responseText;
|
||||
var request = OpenLayers.Request.GET({url: href, async: false});
|
||||
if (request) {
|
||||
return request.responseText;
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -100,7 +100,12 @@ OpenLayers.Layer.GML = OpenLayers.Class(OpenLayers.Layer.Vector, {
|
||||
*/
|
||||
loadGML: function() {
|
||||
if (!this.loaded) {
|
||||
var results = OpenLayers.loadURL(this.url, null, this, this.requestSuccess, this.requestFailure);
|
||||
OpenLayers.Request.GET({
|
||||
url: this.url,
|
||||
success: this.requestSuccess,
|
||||
failure: this.requestFailure,
|
||||
scope: this
|
||||
});
|
||||
this.loaded = true;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -100,7 +100,11 @@ OpenLayers.Layer.GeoRSS = OpenLayers.Class(OpenLayers.Layer.Markers, {
|
||||
loadRSS: function() {
|
||||
if (!this.loaded) {
|
||||
this.events.triggerEvent("loadstart");
|
||||
OpenLayers.loadURL(this.location, null, this, this.parseData);
|
||||
OpenLayers.Request.GET({
|
||||
url: this.location,
|
||||
success: this.parseData,
|
||||
scope: this
|
||||
});
|
||||
this.loaded = true;
|
||||
}
|
||||
},
|
||||
@@ -127,7 +131,7 @@ OpenLayers.Layer.GeoRSS = OpenLayers.Class(OpenLayers.Layer.Markers, {
|
||||
* Parse the data returned from the Events call.
|
||||
*
|
||||
* Parameters:
|
||||
* ajaxRequest - {XMLHttpRequest}
|
||||
* ajaxRequest - {<OpenLayers.Request.XMLHttpRequest>}
|
||||
*/
|
||||
parseData: function(ajaxRequest) {
|
||||
var doc = ajaxRequest.responseXML;
|
||||
|
||||
@@ -202,12 +202,10 @@ OpenLayers.Layer.MapGuide = OpenLayers.Class(OpenLayers.Layer.Grid, {
|
||||
getVisParams.mapName = this.params.mapName;
|
||||
getVisParams.format = 'text/xml';
|
||||
getVisParams = OpenLayers.Util.extend(getVisParams, params);
|
||||
|
||||
new OpenLayers.Ajax.Request(this.url,
|
||||
{ parameters: getVisParams,
|
||||
method: 'get',
|
||||
asynchronous: false //must be synchronous call to return control here
|
||||
});
|
||||
|
||||
OpenLayers.Request.GET({
|
||||
url: this.url, params: getVisParams, async: false
|
||||
});
|
||||
}
|
||||
|
||||
//construct the full URL
|
||||
|
||||
@@ -109,8 +109,12 @@ OpenLayers.Layer.Text = OpenLayers.Class(OpenLayers.Layer.Markers, {
|
||||
};
|
||||
|
||||
this.events.triggerEvent("loadstart");
|
||||
OpenLayers.loadURL(this.location, null,
|
||||
this, this.parseData, onFail);
|
||||
OpenLayers.Request.GET({
|
||||
url: this.location,
|
||||
success: this.parseData,
|
||||
failure: onFail,
|
||||
scope: this
|
||||
});
|
||||
this.loaded = true;
|
||||
}
|
||||
}
|
||||
@@ -137,7 +141,7 @@ OpenLayers.Layer.Text = OpenLayers.Class(OpenLayers.Layer.Markers, {
|
||||
* Method: parseData
|
||||
*
|
||||
* Parameters:
|
||||
* ajaxRequest - {XMLHttpRequest}
|
||||
* ajaxRequest - {<OpenLayers.Request.XMLHttpRequest>}
|
||||
*/
|
||||
parseData: function(ajaxRequest) {
|
||||
var text = ajaxRequest.responseText;
|
||||
|
||||
@@ -455,21 +455,14 @@ OpenLayers.Layer.WFS = OpenLayers.Class(
|
||||
}
|
||||
|
||||
var data = this.writer.write(this.features);
|
||||
|
||||
var url = this.url;
|
||||
|
||||
var success = OpenLayers.Function.bind(this.commitSuccess, this);
|
||||
|
||||
var failure = OpenLayers.Function.bind(this.commitFailure, this);
|
||||
|
||||
// from prototype.js
|
||||
new OpenLayers.Ajax.Request(url,
|
||||
{ method: 'post',
|
||||
postBody: data,
|
||||
onComplete: success,
|
||||
onFailure: failure
|
||||
}
|
||||
);
|
||||
OpenLayers.Request.POST({
|
||||
url: this.url,
|
||||
data: data,
|
||||
success: this.commitSuccess,
|
||||
failure: this.commitFailure,
|
||||
scope: this
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
266
lib/OpenLayers/Request.js
Normal file
266
lib/OpenLayers/Request.js
Normal file
@@ -0,0 +1,266 @@
|
||||
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
|
||||
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
|
||||
* full text of the license. */
|
||||
|
||||
/**
|
||||
* Namespace: OpenLayers.Request
|
||||
* The OpenLayers.Request namespace contains convenience methods for working
|
||||
* with XMLHttpRequests. These methods work with a cross-browser
|
||||
* W3C compliant <OpenLayers.Request.XMLHttpRequest> class.
|
||||
*/
|
||||
OpenLayers.Request = {
|
||||
|
||||
/**
|
||||
* Constant: DEFAULT_CONFIG
|
||||
* {Object} Default configuration for all requests.
|
||||
*/
|
||||
DEFAULT_CONFIG: {
|
||||
method: "GET",
|
||||
url: window.location.href,
|
||||
async: true,
|
||||
user: undefined,
|
||||
password: undefined,
|
||||
params: null,
|
||||
proxy: OpenLayers.ProxyHost,
|
||||
headers: {},
|
||||
data: null,
|
||||
callback: function() {},
|
||||
success: null,
|
||||
failure: null,
|
||||
scope: null
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: issue
|
||||
* Create a new XMLHttpRequest object, open it, set any headers, bind
|
||||
* a callback to done state, and send any data.
|
||||
*
|
||||
* Parameters:
|
||||
* config - {Object} Object containing properties for configuring the
|
||||
* request. Allowed configuration properties are described below.
|
||||
* This object is modified and should not be reused.
|
||||
*
|
||||
* Allowed config properties:
|
||||
* method - {String} One of GET, POST, PUT, DELETE, HEAD, or
|
||||
* OPTIONS. Default is GET.
|
||||
* url - {String} URL for the request.
|
||||
* async - {Boolean} Open an asynchronous request. Default is true.
|
||||
* user - {String} User for relevant authentication scheme. Set
|
||||
* to null to clear current user.
|
||||
* password - {String} Password for relevant authentication scheme.
|
||||
* Set to null to clear current password.
|
||||
* proxy - {String} Optional proxy. Defaults to
|
||||
* <OpenLayers.ProxyHost>.
|
||||
* params - {Object} Any key:value pairs to be appended to the
|
||||
* url as a query string. Assumes url doesn't already include a query
|
||||
* string or hash. Parameter values that are arrays will be
|
||||
* concatenated with a comma (note that this goes against form-encoding)
|
||||
* as is done with <OpenLayers.Util.getParameterString>.
|
||||
* headers - {Object} Object with header:value pairs to be set on
|
||||
* the request.
|
||||
* data - {Object} Any data to send with the request.
|
||||
* callback - {Function} Function to call when request is done.
|
||||
* To determine if the request failed, check request.status (200
|
||||
* indicates success).
|
||||
* success - {Function} Optional function to call if request status is in
|
||||
* the 200s. This will be called in addition to callback above and
|
||||
* would typically only be used as an alternative.
|
||||
* failure - {Function} Optional function to call if request status is not
|
||||
* in the 200s. This will be called in addition to callback above and
|
||||
* would typically only be used as an alternative.
|
||||
* scope - {Object} If callback is a public method on some object,
|
||||
* set the scope to that object.
|
||||
*
|
||||
* Returns:
|
||||
* {XMLHttpRequest} Request object. To abort the request before a response
|
||||
* is received, call abort() on the request object.
|
||||
*/
|
||||
issue: function(config) {
|
||||
// apply default config - proxy host may have changed
|
||||
var defaultConfig = OpenLayers.Util.extend(
|
||||
this.DEFAULT_CONFIG,
|
||||
{proxy: OpenLayers.ProxyHost}
|
||||
);
|
||||
config = OpenLayers.Util.applyDefaults(config, defaultConfig);
|
||||
|
||||
// create request, open, and set headers
|
||||
var request = new OpenLayers.Request.XMLHttpRequest();
|
||||
var url = config.url;
|
||||
if(config.params) {
|
||||
url += "?" + OpenLayers.Util.getParameterString(config.params);
|
||||
}
|
||||
if(config.proxy && (url.indexOf("http") == 0)) {
|
||||
url = config.proxy + encodeURIComponent(url);
|
||||
}
|
||||
request.open(
|
||||
config.method, url, config.async, config.user, config.password
|
||||
);
|
||||
for(var header in config.headers) {
|
||||
request.setRequestHeader(header, config.headers[header]);
|
||||
}
|
||||
|
||||
// bind callbacks to readyState 4 (done)
|
||||
var complete = (config.scope) ?
|
||||
OpenLayers.Function.bind(config.callback, config.scope) :
|
||||
config.callback;
|
||||
|
||||
// optional success callback
|
||||
var success;
|
||||
if(config.success) {
|
||||
success = (config.scope) ?
|
||||
OpenLayers.Function.bind(config.success, config.scope) :
|
||||
config.success;
|
||||
}
|
||||
|
||||
// optional failure callback
|
||||
var failure;
|
||||
if(config.failure) {
|
||||
failure = (config.scope) ?
|
||||
OpenLayers.Function.bind(config.failure, config.scope) :
|
||||
config.failure;
|
||||
}
|
||||
|
||||
request.onreadystatechange = function() {
|
||||
if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) {
|
||||
complete(request);
|
||||
if(success && request.status >= 200 && request.status < 300) {
|
||||
success(request);
|
||||
}
|
||||
if(failure && (request.status < 200 || request.status >= 300)) {
|
||||
failure(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// send request (optionally with data) and return
|
||||
request.send(config.data);
|
||||
return request;
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: GET
|
||||
* Send an HTTP GET request. Additional configuration properties are
|
||||
* documented in the <issue> method, with the method property set
|
||||
* to GET.
|
||||
*
|
||||
* Parameters:
|
||||
* config - {Object} Object with properties for configuring the request.
|
||||
* See the <issue> method for documentation of allowed properties.
|
||||
* This object is modified and should not be reused.
|
||||
*
|
||||
* Returns:
|
||||
* {XMLHttpRequest} Request object.
|
||||
*/
|
||||
GET: function(config) {
|
||||
config = OpenLayers.Util.extend(config, {method: "GET"});
|
||||
return OpenLayers.Request.issue(config);
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: POST
|
||||
* Send a POST request. Additional configuration properties are
|
||||
* documented in the <issue> method, with the method property set
|
||||
* to POST and "Content-Type" header set to "application/xml".
|
||||
*
|
||||
* Parameters:
|
||||
* config - {Object} Object with properties for configuring the request.
|
||||
* See the <issue> method for documentation of allowed properties. The
|
||||
* default "Content-Type" header will be set to "application-xml" if
|
||||
* none is provided. This object is modified and should not be reused.
|
||||
*
|
||||
* Returns:
|
||||
* {XMLHttpRequest} Request object.
|
||||
*/
|
||||
POST: function(config) {
|
||||
config = OpenLayers.Util.extend(config, {method: "POST"});
|
||||
// set content type to application/xml if it isn't already set
|
||||
config.headers = config.headers ? config.headers : {};
|
||||
if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) {
|
||||
config.headers["Content-Type"] = "application/xml";
|
||||
}
|
||||
return OpenLayers.Request.issue(config);
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: PUT
|
||||
* Send an HTTP PUT request. Additional configuration properties are
|
||||
* documented in the <issue> method, with the method property set
|
||||
* to PUT and "Content-Type" header set to "application/xml".
|
||||
*
|
||||
* Parameters:
|
||||
* config - {Object} Object with properties for configuring the request.
|
||||
* See the <issue> method for documentation of allowed properties. The
|
||||
* default "Content-Type" header will be set to "application-xml" if
|
||||
* none is provided. This object is modified and should not be reused.
|
||||
*
|
||||
* Returns:
|
||||
* {XMLHttpRequest} Request object.
|
||||
*/
|
||||
PUT: function(config) {
|
||||
config = OpenLayers.Util.extend(config, {method: "PUT"});
|
||||
// set content type to application/xml if it isn't already set
|
||||
config.headers = config.headers ? config.headers : {};
|
||||
if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) {
|
||||
config.headers["Content-Type"] = "application/xml";
|
||||
}
|
||||
return OpenLayers.Request.issue(config);
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: DELETE
|
||||
* Send an HTTP DELETE request. Additional configuration properties are
|
||||
* documented in the <issue> method, with the method property set
|
||||
* to DELETE.
|
||||
*
|
||||
* Parameters:
|
||||
* config - {Object} Object with properties for configuring the request.
|
||||
* See the <issue> method for documentation of allowed properties.
|
||||
* This object is modified and should not be reused.
|
||||
*
|
||||
* Returns:
|
||||
* {XMLHttpRequest} Request object.
|
||||
*/
|
||||
DELETE: function(config) {
|
||||
config = OpenLayers.Util.extend(config, {method: "DELETE"});
|
||||
return OpenLayers.Request.issue(config);
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: HEAD
|
||||
* Send an HTTP HEAD request. Additional configuration properties are
|
||||
* documented in the <issue> method, with the method property set
|
||||
* to HEAD.
|
||||
*
|
||||
* Parameters:
|
||||
* config - {Object} Object with properties for configuring the request.
|
||||
* See the <issue> method for documentation of allowed properties.
|
||||
* This object is modified and should not be reused.
|
||||
*
|
||||
* Returns:
|
||||
* {XMLHttpRequest} Request object.
|
||||
*/
|
||||
HEAD: function(config) {
|
||||
config = OpenLayers.Util.extend(config, {method: "HEAD"});
|
||||
return OpenLayers.Request.issue(config);
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: OPTIONS
|
||||
* Send an HTTP OPTIONS request. Additional configuration properties are
|
||||
* documented in the <issue> method, with the method property set
|
||||
* to OPTIONS.
|
||||
*
|
||||
* Parameters:
|
||||
* config - {Object} Object with properties for configuring the request.
|
||||
* See the <issue> method for documentation of allowed properties.
|
||||
* This object is modified and should not be reused.
|
||||
*
|
||||
* Returns:
|
||||
* {XMLHttpRequest} Request object.
|
||||
*/
|
||||
OPTIONS: function(config) {
|
||||
config = OpenLayers.Util.extend(config, {method: "OPTIONS"});
|
||||
return OpenLayers.Request.issue(config);
|
||||
}
|
||||
|
||||
};
|
||||
313
lib/OpenLayers/Request/XMLHttpRequest.js
Normal file
313
lib/OpenLayers/Request/XMLHttpRequest.js
Normal file
@@ -0,0 +1,313 @@
|
||||
// Copyright 2007 Sergey Ilinsky (http://www.ilinsky.com)
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/**
|
||||
* @requires OpenLayers/Request.js
|
||||
*/
|
||||
|
||||
(function () {
|
||||
|
||||
// Save reference to earlier defined object implementation (if any)
|
||||
var oXMLHttpRequest = window.XMLHttpRequest;
|
||||
|
||||
// Define on browser type
|
||||
var bGecko = !!window.controllers,
|
||||
bIE = window.document.all && !window.opera;
|
||||
|
||||
// Constructor
|
||||
function cXMLHttpRequest() {
|
||||
this._object = oXMLHttpRequest ? new oXMLHttpRequest : new window.ActiveXObject('Microsoft.XMLHTTP');
|
||||
};
|
||||
|
||||
// BUGFIX: Firefox with Firebug installed would break pages if not executed
|
||||
if (bGecko && oXMLHttpRequest.wrapped)
|
||||
cXMLHttpRequest.wrapped = oXMLHttpRequest.wrapped;
|
||||
|
||||
// Constants
|
||||
cXMLHttpRequest.UNSENT = 0;
|
||||
cXMLHttpRequest.OPENED = 1;
|
||||
cXMLHttpRequest.HEADERS_RECEIVED = 2;
|
||||
cXMLHttpRequest.LOADING = 3;
|
||||
cXMLHttpRequest.DONE = 4;
|
||||
|
||||
// Public Properties
|
||||
cXMLHttpRequest.prototype.readyState = cXMLHttpRequest.UNSENT;
|
||||
cXMLHttpRequest.prototype.responseText = "";
|
||||
cXMLHttpRequest.prototype.responseXML = null;
|
||||
cXMLHttpRequest.prototype.status = 0;
|
||||
cXMLHttpRequest.prototype.statusText = "";
|
||||
|
||||
// Instance-level Events Handlers
|
||||
cXMLHttpRequest.prototype.onreadystatechange = null;
|
||||
|
||||
// Class-level Events Handlers
|
||||
cXMLHttpRequest.onreadystatechange = null;
|
||||
cXMLHttpRequest.onopen = null;
|
||||
cXMLHttpRequest.onsend = null;
|
||||
cXMLHttpRequest.onabort = null;
|
||||
|
||||
// Public Methods
|
||||
cXMLHttpRequest.prototype.open = function(sMethod, sUrl, bAsync, sUser, sPassword) {
|
||||
|
||||
// Save async parameter for fixing Gecko bug with missing readystatechange in synchronous requests
|
||||
this._async = bAsync;
|
||||
|
||||
// Set the onreadystatechange handler
|
||||
var oRequest = this,
|
||||
nState = this.readyState;
|
||||
|
||||
// BUGFIX: IE - memory leak on page unload (inter-page leak)
|
||||
if (bIE) {
|
||||
var fOnUnload = function() {
|
||||
if (oRequest._object.readyState != cXMLHttpRequest.DONE)
|
||||
fCleanTransport(oRequest);
|
||||
};
|
||||
if (bAsync)
|
||||
window.attachEvent("onunload", fOnUnload);
|
||||
}
|
||||
|
||||
this._object.onreadystatechange = function() {
|
||||
if (bGecko && !bAsync)
|
||||
return;
|
||||
|
||||
// Synchronize state
|
||||
oRequest.readyState = oRequest._object.readyState;
|
||||
|
||||
//
|
||||
fSynchronizeValues(oRequest);
|
||||
|
||||
// BUGFIX: Firefox fires unneccesary DONE when aborting
|
||||
if (oRequest._aborted) {
|
||||
// Reset readyState to UNSENT
|
||||
oRequest.readyState = cXMLHttpRequest.UNSENT;
|
||||
|
||||
// Return now
|
||||
return;
|
||||
}
|
||||
|
||||
if (oRequest.readyState == cXMLHttpRequest.DONE) {
|
||||
//
|
||||
fCleanTransport(oRequest);
|
||||
// Uncomment this block if you need a fix for IE cache
|
||||
/*
|
||||
// BUGFIX: IE - cache issue
|
||||
if (!oRequest._object.getResponseHeader("Date")) {
|
||||
// Save object to cache
|
||||
oRequest._cached = oRequest._object;
|
||||
|
||||
// Instantiate a new transport object
|
||||
cXMLHttpRequest.call(oRequest);
|
||||
|
||||
// Re-send request
|
||||
oRequest._object.open(sMethod, sUrl, bAsync, sUser, sPassword);
|
||||
oRequest._object.setRequestHeader("If-Modified-Since", oRequest._cached.getResponseHeader("Last-Modified") || new window.Date(0));
|
||||
// Copy headers set
|
||||
if (oRequest._headers)
|
||||
for (var sHeader in oRequest._headers)
|
||||
if (typeof oRequest._headers[sHeader] == "string") // Some frameworks prototype objects with functions
|
||||
oRequest._object.setRequestHeader(sHeader, oRequest._headers[sHeader]);
|
||||
|
||||
oRequest._object.onreadystatechange = function() {
|
||||
// Synchronize state
|
||||
oRequest.readyState = oRequest._object.readyState;
|
||||
|
||||
if (oRequest._aborted) {
|
||||
//
|
||||
oRequest.readyState = cXMLHttpRequest.UNSENT;
|
||||
|
||||
// Return
|
||||
return;
|
||||
}
|
||||
|
||||
if (oRequest.readyState == cXMLHttpRequest.DONE) {
|
||||
// Clean Object
|
||||
fCleanTransport(oRequest);
|
||||
|
||||
// get cached request
|
||||
if (oRequest.status == 304)
|
||||
oRequest._object = oRequest._cached;
|
||||
|
||||
//
|
||||
delete oRequest._cached;
|
||||
|
||||
//
|
||||
fSynchronizeValues(oRequest);
|
||||
|
||||
//
|
||||
fReadyStateChange(oRequest);
|
||||
|
||||
// BUGFIX: IE - memory leak in interrupted
|
||||
if (bIE && bAsync)
|
||||
window.detachEvent("onunload", fOnUnload);
|
||||
}
|
||||
};
|
||||
oRequest._object.send(null);
|
||||
|
||||
// Return now - wait untill re-sent request is finished
|
||||
return;
|
||||
};
|
||||
*/
|
||||
// BUGFIX: IE - memory leak in interrupted
|
||||
if (bIE && bAsync)
|
||||
window.detachEvent("onunload", fOnUnload);
|
||||
}
|
||||
|
||||
// BUGFIX: Some browsers (Internet Explorer, Gecko) fire OPEN readystate twice
|
||||
if (nState != oRequest.readyState)
|
||||
fReadyStateChange(oRequest);
|
||||
|
||||
nState = oRequest.readyState;
|
||||
};
|
||||
|
||||
// Add method sniffer
|
||||
if (cXMLHttpRequest.onopen)
|
||||
cXMLHttpRequest.onopen.apply(this, arguments);
|
||||
|
||||
this._object.open(sMethod, sUrl, bAsync, sUser, sPassword);
|
||||
|
||||
// BUGFIX: Gecko - missing readystatechange calls in synchronous requests
|
||||
if (!bAsync && bGecko) {
|
||||
this.readyState = cXMLHttpRequest.OPENED;
|
||||
|
||||
fReadyStateChange(this);
|
||||
}
|
||||
};
|
||||
cXMLHttpRequest.prototype.send = function(vData) {
|
||||
// Add method sniffer
|
||||
if (cXMLHttpRequest.onsend)
|
||||
cXMLHttpRequest.onsend.apply(this, arguments);
|
||||
|
||||
// BUGFIX: Safari - fails sending documents created/modified dynamically, so an explicit serialization required
|
||||
// BUGFIX: IE - rewrites any custom mime-type to "text/xml" in case an XMLNode is sent
|
||||
// BUGFIX: Gecko - fails sending Element (this is up to the implementation either to standard)
|
||||
if (vData && vData.nodeType) {
|
||||
vData = window.XMLSerializer ? new window.XMLSerializer().serializeToString(vData) : vData.xml;
|
||||
if (!this._headers["Content-Type"])
|
||||
this._object.setRequestHeader("Content-Type", "application/xml");
|
||||
}
|
||||
|
||||
this._object.send(vData);
|
||||
|
||||
// BUGFIX: Gecko - missing readystatechange calls in synchronous requests
|
||||
if (bGecko && !this._async) {
|
||||
this.readyState = cXMLHttpRequest.OPENED;
|
||||
|
||||
// Synchronize state
|
||||
fSynchronizeValues(this);
|
||||
|
||||
// Simulate missing states
|
||||
while (this.readyState < cXMLHttpRequest.DONE) {
|
||||
this.readyState++;
|
||||
fReadyStateChange(this);
|
||||
// Check if we are aborted
|
||||
if (this._aborted)
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
cXMLHttpRequest.prototype.abort = function() {
|
||||
// Add method sniffer
|
||||
if (cXMLHttpRequest.onabort)
|
||||
cXMLHttpRequest.onabort.apply(this, arguments);
|
||||
|
||||
// BUGFIX: Gecko - unneccesary DONE when aborting
|
||||
if (this.readyState > cXMLHttpRequest.UNSENT)
|
||||
this._aborted = true;
|
||||
|
||||
this._object.abort();
|
||||
|
||||
// BUGFIX: IE - memory leak
|
||||
fCleanTransport(this);
|
||||
};
|
||||
cXMLHttpRequest.prototype.getAllResponseHeaders = function() {
|
||||
return this._object.getAllResponseHeaders();
|
||||
};
|
||||
cXMLHttpRequest.prototype.getResponseHeader = function(sName) {
|
||||
return this._object.getResponseHeader(sName);
|
||||
};
|
||||
cXMLHttpRequest.prototype.setRequestHeader = function(sName, sValue) {
|
||||
// BUGFIX: IE - cache issue
|
||||
if (!this._headers)
|
||||
this._headers = {};
|
||||
this._headers[sName] = sValue;
|
||||
|
||||
return this._object.setRequestHeader(sName, sValue);
|
||||
};
|
||||
cXMLHttpRequest.prototype.toString = function() {
|
||||
return '[' + "object" + ' ' + "XMLHttpRequest" + ']';
|
||||
};
|
||||
cXMLHttpRequest.toString = function() {
|
||||
return '[' + "XMLHttpRequest" + ']';
|
||||
};
|
||||
|
||||
// Helper function
|
||||
function fReadyStateChange(oRequest) {
|
||||
// Execute onreadystatechange
|
||||
if (oRequest.onreadystatechange)
|
||||
oRequest.onreadystatechange.apply(oRequest);
|
||||
|
||||
// Sniffing code
|
||||
if (cXMLHttpRequest.onreadystatechange)
|
||||
cXMLHttpRequest.onreadystatechange.apply(oRequest);
|
||||
};
|
||||
|
||||
function fGetDocument(oRequest) {
|
||||
var oDocument = oRequest.responseXML;
|
||||
// Try parsing responseText
|
||||
if (bIE && oDocument && !oDocument.documentElement && oRequest.getResponseHeader("Content-Type").match(/[^\/]+\/[^\+]+\+xml/)) {
|
||||
oDocument = new ActiveXObject('Microsoft.XMLDOM');
|
||||
oDocument.loadXML(oRequest.responseText);
|
||||
}
|
||||
// Check if there is no error in document
|
||||
if (oDocument)
|
||||
if ((bIE && oDocument.parseError != 0) || (oDocument.documentElement && oDocument.documentElement.tagName == "parsererror"))
|
||||
return null;
|
||||
return oDocument;
|
||||
};
|
||||
|
||||
function fSynchronizeValues(oRequest) {
|
||||
try { oRequest.responseText = oRequest._object.responseText; } catch (e) {}
|
||||
try { oRequest.responseXML = fGetDocument(oRequest._object); } catch (e) {}
|
||||
try { oRequest.status = oRequest._object.status; } catch (e) {}
|
||||
try { oRequest.statusText = oRequest._object.statusText; } catch (e) {}
|
||||
};
|
||||
|
||||
function fCleanTransport(oRequest) {
|
||||
// BUGFIX: IE - memory leak (on-page leak)
|
||||
oRequest._object.onreadystatechange = new window.Function;
|
||||
|
||||
// Delete private properties
|
||||
delete oRequest._headers;
|
||||
};
|
||||
|
||||
// Internet Explorer 5.0 (missing apply)
|
||||
if (!window.Function.prototype.apply) {
|
||||
window.Function.prototype.apply = function(oRequest, oArguments) {
|
||||
if (!oArguments)
|
||||
oArguments = [];
|
||||
oRequest.__func = this;
|
||||
oRequest.__func(oArguments[0], oArguments[1], oArguments[2], oArguments[3], oArguments[4]);
|
||||
delete oRequest.__func;
|
||||
};
|
||||
};
|
||||
|
||||
// Register new object with window
|
||||
/**
|
||||
* Class: OpenLayers.Request.XMLHttpRequest
|
||||
* Standard-compliant (W3C) cross-browser implementation of the
|
||||
* XMLHttpRequest object. From
|
||||
* http://code.google.com/p/xmlhttprequest/.
|
||||
*/
|
||||
OpenLayers.Request.XMLHttpRequest = cXMLHttpRequest;
|
||||
})();
|
||||
@@ -32,7 +32,7 @@ OpenLayers.Tile.WFS = OpenLayers.Class(OpenLayers.Tile, {
|
||||
|
||||
/**
|
||||
* Property: request
|
||||
* {OpenLayers.Ajax.Request}
|
||||
* {<OpenLayers.Request.XMLHttpRequest>}
|
||||
*/
|
||||
request: null,
|
||||
|
||||
@@ -100,8 +100,7 @@ OpenLayers.Tile.WFS = OpenLayers.Class(OpenLayers.Tile, {
|
||||
|
||||
/**
|
||||
* Method: loadFeaturesForRegion
|
||||
* get the full request string from the ds and the tile params
|
||||
* and call the AJAX loadURL().
|
||||
* Abort any pending requests and issue another request for data.
|
||||
*
|
||||
* Input are function pointers for what to do on success and failure.
|
||||
*
|
||||
@@ -113,7 +112,12 @@ OpenLayers.Tile.WFS = OpenLayers.Class(OpenLayers.Tile, {
|
||||
if(this.request) {
|
||||
this.request.abort();
|
||||
}
|
||||
this.request = OpenLayers.loadURL(this.url, null, this, success);
|
||||
this.request = OpenLayers.Request.GET({
|
||||
url: this.url,
|
||||
success: success,
|
||||
failure: failure,
|
||||
scope: this
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -122,7 +126,7 @@ OpenLayers.Tile.WFS = OpenLayers.Class(OpenLayers.Tile, {
|
||||
* layer.addFeatures in vector mode, addResults otherwise.
|
||||
*
|
||||
* Parameters:
|
||||
* request - {XMLHttpRequest}
|
||||
* request - {<OpenLayers.Request.XMLHttpRequest>}
|
||||
*/
|
||||
requestSuccess:function(request) {
|
||||
if (this.features) {
|
||||
|
||||
Reference in New Issue
Block a user