Improve XHR and ol.net.jsonp result/failure handling
This commit is contained in:
@@ -75,7 +75,7 @@ ol.featureloader.loadFeaturesXhr = function(url, format, success, failure) {
|
||||
* @private
|
||||
*/
|
||||
xhr.onload = function(event) {
|
||||
if (xhr.status < 400) {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
var type = format.getType();
|
||||
/** @type {Document|Node|Object|string|undefined} */
|
||||
var source;
|
||||
|
||||
@@ -14,19 +14,23 @@ goog.provide('ol.net');
|
||||
*/
|
||||
ol.net.jsonp = function(url, callback, opt_errback, opt_callbackParam) {
|
||||
var script = goog.global.document.createElement('script');
|
||||
script.async = true;
|
||||
var key = 'olc_' + goog.getUid(callback);
|
||||
function cleanup() {
|
||||
delete goog.global[key];
|
||||
script.parentNode.removeChild(script);
|
||||
}
|
||||
script.async = true;
|
||||
script.src = url + (url.indexOf('?') == -1 ? '?' : '&') +
|
||||
(opt_callbackParam || 'callback') + '=' + key;
|
||||
var timer = goog.global.setTimeout(function() {
|
||||
delete goog.global[key];
|
||||
cleanup();
|
||||
if (opt_errback) {
|
||||
opt_errback();
|
||||
}
|
||||
}, 10000);
|
||||
goog.global[key] = function(data) {
|
||||
goog.global.clearTimeout(timer);
|
||||
delete goog.global[key];
|
||||
cleanup();
|
||||
callback(data);
|
||||
};
|
||||
goog.global.document.getElementsByTagName('head')[0].appendChild(script);
|
||||
|
||||
@@ -46,7 +46,7 @@ ol.source.TileJSON = function(options) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', options.url, true);
|
||||
xhr.onload = function(e) {
|
||||
if (xhr.status < 400) {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
var response = /** @type {TileJSON} */(JSON.parse(xhr.responseText));
|
||||
this.handleTileJSONResponse(response);
|
||||
} else {
|
||||
|
||||
@@ -6,12 +6,15 @@ describe('ol.net', function() {
|
||||
describe('jsonp()', function() {
|
||||
var head = goog.global.document.getElementsByTagName('head')[0];
|
||||
var origAppendChild = head.appendChild;
|
||||
var origCreateElement = document.createElement;
|
||||
var origSetTimeout = goog.global.setTimeout;
|
||||
var key;
|
||||
var key, removeChild;
|
||||
|
||||
function createCallback(url, done) {
|
||||
removeChild = sinon.spy();
|
||||
var callback = function(data) {
|
||||
expect(data).to.be(url + key);
|
||||
expect(removeChild.called).to.be(true);
|
||||
done();
|
||||
};
|
||||
key = 'olc_' + goog.getUid(callback);
|
||||
@@ -19,9 +22,15 @@ describe('ol.net', function() {
|
||||
}
|
||||
|
||||
beforeEach(function() {
|
||||
document.createElement = function() {
|
||||
return {}
|
||||
};
|
||||
head.appendChild = function(element) {
|
||||
element.parentNode = {
|
||||
removeChild: removeChild
|
||||
};
|
||||
origSetTimeout(function() {
|
||||
goog.global[key](element.getAttribute('src'));
|
||||
goog.global[key](element.src);
|
||||
}, 0);
|
||||
};
|
||||
goog.global.setTimeout = function(fn, time) {
|
||||
@@ -30,6 +39,7 @@ describe('ol.net', function() {
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
document.createElement = origCreateElement;
|
||||
head.appendChild = origAppendChild;
|
||||
goog.global.setTimeout = origSetTimeout;
|
||||
});
|
||||
@@ -42,12 +52,17 @@ describe('ol.net', function() {
|
||||
ol.net.jsonp('http://foo/bar?baz', callback);
|
||||
});
|
||||
it('calls errback when jsonp is not executed, cleans up', function(done) {
|
||||
head.appendChild = function() {};
|
||||
head.appendChild = function(element) {
|
||||
element.parentNode = {
|
||||
removeChild: removeChild
|
||||
};
|
||||
};
|
||||
function callback() {
|
||||
expect.fail();
|
||||
}
|
||||
function errback() {
|
||||
expect(goog.global[key]).to.be(undefined);
|
||||
expect(removeChild.called).to.be(true);
|
||||
done();
|
||||
}
|
||||
ol.net.jsonp('foo', callback, errback);
|
||||
|
||||
Reference in New Issue
Block a user