2gis-maps
Version:
Interactive 2GIS maps API, based on Leaflet
113 lines (101 loc) • 3.65 kB
HTML
<html>
<head>
<title>DGAjax & DGPromise demo</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://maps.api.2gis.ru/2.0/loader.js"></script>
</head>
<body>
<div id="map" style="width: 100%; height: 400px; border: 1px solid #ccc;"></div>
<script type="text/javascript">
// Avoid `console` errors in browsers that lack a console.
(function() {
var method;
var noop = function () {};
var methods = [
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
'timeStamp', 'trace', 'warn'
];
var length = methods.length;
var console = (window.console = window.console || {});
while (length--) {
method = methods[length];
if (!console[method]) {
console[method] = noop;
}
}
}());
DG.then(function () {
var map = new DG.Map('map', {
center: new DG.LatLng(54.980206086231, 82.898068362003),
zoom: 17,
geoclicker: true,
key: DG.config.key || 'Your API access key'
});
function test_get(){
console.group('Test XHR GET');
return DG.ajax('http://catalog.api.2gis.ru/2.0/search', {
type: 'get',
data: {
key: 'ruregt3044',
type: 'filial',
house: 141373143572328,
page: 1
},
success: function(data) {
console.log('success', data);
},
error: function(error) {
console.warn('error', error);
}
}).then(
function(data) {
console.log('promise resolve', data);
console.groupEnd();
},
function(error) {
console.warn('promise reject', error);
console.groupEnd();
}
);
}
function test_post(){
console.group('Test XHR POST');
return DG.ajax('http://catalog.api.2gis.ru/2.0/search', {
type: 'post',
data: {
key: 'ruregt3044',
type: 'filial',
house: 141373143572328,
page: 1
},
success: function(data) {
console.log('success', data);
},
error: function(error) {
console.warn('error', error);
}
}).then(
function(data) {
console.log('promise resolve', data);
console.groupEnd();
},
function(error) {
console.warn('promise reject', error);
console.groupEnd();
}
);
}
DG.DomEvent.addListener( DG.DomUtil.get('sendRequest'), 'click', function(){
test_get().then(test_post).then(function(){
console.info('Everything seems to be ok!');
});
});
});
</script>
<button id="sendRequest">Сделать запрос</button>
</body>
</html>