chore: catch error when using Request, and make remote URL working again

I decided to remove the check `is_ajax` from `validate_url` to simplify
and edge case, and because I think it was more or less useless.
Basically, when getting remote data, we have two cases:
- direct call to the remote URL
- proxy through our `ajax_proxy` system (to work around CORS limitations)

In the first case, we cannot set the `X-Requested-With` header, otherwise
preflight step will fail, and in the second case, until now, we needed
to set this header for this `is_ajax` check to pass. So keeping this check
would mean adapting the behaviour of the Request/ServerRequest class in
a non elegant way. So let's make it simple…
This commit is contained in:
Yohan Boniface 2024-01-26 13:16:12 +01:00
parent 5926eb53ba
commit da7d09527b
2 changed files with 13 additions and 9 deletions

View file

@ -747,15 +747,18 @@ L.U.DataLayer = L.Evented.extend({
if (!this.options.remoteData.dynamic && this.hasDataLoaded() && !force) return if (!this.options.remoteData.dynamic && this.hasDataLoaded() && !force) return
if (!this.isVisible()) return if (!this.isVisible()) return
let url = this.map.localizeUrl(this.options.remoteData.url) let url = this.map.localizeUrl(this.options.remoteData.url)
if (this.options.remoteData.proxy) if (this.options.remoteData.proxy) {
url = this.map.proxyUrl(url, this.options.remoteData.ttl) url = this.map.proxyUrl(url, this.options.remoteData.ttl)
}
const response = await this.map.request.get(url) const response = await this.map.request.get(url)
this.clear() if (response && response.ok) {
this.rawToGeoJSON( this.clear()
await response.text(), this.rawToGeoJSON(
this.options.remoteData.format, await response.text(),
(geojson) => this.fromGeoJSON(geojson) this.options.remoteData.format,
) (geojson) => this.fromGeoJSON(geojson)
)
}
}, },
onceLoaded: function (callback, context) { onceLoaded: function (callback, context) {
@ -1060,7 +1063,9 @@ L.U.DataLayer = L.Evented.extend({
importFromUrl: async function (uri, type) { importFromUrl: async function (uri, type) {
uri = this.map.localizeUrl(uri) uri = this.map.localizeUrl(uri)
const response = await this.map.request.get(uri) const response = await this.map.request.get(uri)
this.importRaw(await response.text(), type) if (response && response.ok) {
this.importRaw(await response.text(), type)
}
}, },
getColor: function () { getColor: function () {

View file

@ -335,7 +335,6 @@ showcase = MapsShowCase.as_view()
def validate_url(request): def validate_url(request):
assert request.method == "GET" assert request.method == "GET"
assert is_ajax(request)
url = request.GET.get("url") url = request.GET.get("url")
assert url assert url
try: try: