diff --git a/umap/static/umap/js/modules/request.js b/umap/static/umap/js/modules/request.js index a77ab47b..a3aaf158 100644 --- a/umap/static/umap/js/modules/request.js +++ b/umap/static/umap/js/modules/request.js @@ -63,6 +63,14 @@ export const Request = BaseRequest.extend({ // Adds uMap specifics to requests handling // like logging, CSRF, etc. export const ServerRequest = Request.extend({ + _fetch: async function (method, uri, headers, data) { + // Add a flag so backend can know we are in ajax and adapt the response + // See is_ajax in utils.py + headers = headers || {} + headers['X-Requested-With'] = 'XMLHttpRequest' + return await Request.prototype._fetch.call(this, method, uri, headers, data) + }, + post: async function (uri, headers, data) { const token = document.cookie.replace( /(?:(?:^|.*;\s*)csrftoken\s*\=\s*([^;]*).*$)|^.*$/, diff --git a/umap/templates/umap/content.html b/umap/templates/umap/content.html index 2eccf4d1..6647095d 100644 --- a/umap/templates/umap/content.html +++ b/umap/templates/umap/content.html @@ -36,33 +36,24 @@ {% block bottom_js %} {{ block.super }} {% endblock bottom_js %} {% block footer %} diff --git a/umap/views.py b/umap/views.py index 28816b6e..b01eedc0 100644 --- a/umap/views.py +++ b/umap/views.py @@ -108,6 +108,12 @@ class PaginatorMixin: return [self.list_template_name] return super().get_template_names() + def get(self, *args, **kwargs): + response = super().get(*args, **kwargs) + if is_ajax(self.request): + return simple_json_response(html=response.rendered_content) + return response + class PublicMapsMixin(object): def get_public_maps(self):