package.net.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ol Show documentation
Show all versions of ol Show documentation
OpenLayers mapping library
The newest version!
/**
* @module ol/net
*/
import {getUid} from './util.js';
/**
* Simple JSONP helper. Supports error callbacks and a custom callback param.
* The error callback will be called when no JSONP is executed after 10 seconds.
*
* @param {string} url Request url. A 'callback' query parameter will be
* appended.
* @param {Function} callback Callback on success.
* @param {Function} [errback] Callback on error.
* @param {string} [callbackParam] Custom query parameter for the JSONP
* callback. Default is 'callback'.
*/
export function jsonp(url, callback, errback, callbackParam) {
const script = document.createElement('script');
const key = 'olc_' + getUid(callback);
function cleanup() {
delete window[key];
script.parentNode.removeChild(script);
}
script.async = true;
script.src =
url +
(url.includes('?') ? '&' : '?') +
(callbackParam || 'callback') +
'=' +
key;
const timer = setTimeout(function () {
cleanup();
if (errback) {
errback();
}
}, 10000);
window[key] = function (data) {
clearTimeout(timer);
cleanup();
callback(data);
};
document.head.appendChild(script);
}
export class ResponseError extends Error {
/**
* @param {XMLHttpRequest} response The XHR object.
*/
constructor(response) {
const message = 'Unexpected response status: ' + response.status;
super(message);
/**
* @type {string}
*/
this.name = 'ResponseError';
/**
* @type {XMLHttpRequest}
*/
this.response = response;
}
}
export class ClientError extends Error {
/**
* @param {XMLHttpRequest} client The XHR object.
*/
constructor(client) {
super('Failed to issue request');
/**
* @type {string}
*/
this.name = 'ClientError';
/**
* @type {XMLHttpRequest}
*/
this.client = client;
}
}
/**
* @param {string} url The URL.
* @return {Promise