perliedman / leaflet-control-geocoder

A simple geocoder form to locate places. Easily extended to multiple data providers.
http://www.liedman.net/leaflet-control-geocoder/
BSD 2-Clause "Simplified" License
560 stars 220 forks source link

406 and CORS violation when using self-hosted Nominatim #336

Open oldwired opened 2 years ago

oldwired commented 2 years ago

When trying to use the plugin with my own instance of nominatim (behind a caddy reverse proxy), which works via browser address bar and Postman, I get the following errors:

geocode.html:1 

       Access to XMLHttpRequest at 'https://geocoder.leitstelle.cloud/search?q=Heiligenbreite%206%2C%2088662%20%C3%9Cberlingen&limit=5&format=json&addressdetails=1' from origin 'http://localhost:63342' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
util.ts:109 

       GET https://geocoder.leitstelle.cloud/search?q=Heiligenbreite%206%2C%2088662%20%C3%9Cberlingen&limit=5&format=json&addressdetails=1 net::ERR_FAILED 406
getJSON @ util.ts:109
geocode @ nominatim.ts:102
_geocode @ control.ts:336
_keydown @ control.ts:462
o @ DomEvent.js:108

My code:

    var geocoder = L.Control.Geocoder.nominatim(
        {
        serviceUrl: "https://geocoder.leitstelle.cloud/"
    });
    var control = L.Control.geocoder({
        placeholder: 'Search here...',
        geocoder: geocoder
    }).addTo(map);

General:

Request URL: https://geocoder.leitstelle.cloud/search?q=Heiligenbreite%206%2C%2088662%20%C3%9Cberlingen&limit=5&format=json&addressdetails=1
Request Method: GET
Status Code: 406 
Referrer Policy: strict-origin-when-cross-origin

Response Headers:

alternates: {"search.php" 1 {type text/html}}
content-length: 435
content-type: text/html; charset=iso-8859-1
date: Mon, 27 Jun 2022 20:31:18 GMT
server: Caddy
server: Apache/2.4.41 (Ubuntu)
tcn: list
vary: negotiate

Request Headers:

:authority: geocoder.leitstelle.cloud
:method: GET
:path: /search?q=Heiligenbreite%206%2C%2088662%20%C3%9Cberlingen&limit=5&format=json&addressdetails=1
:scheme: https
accept: application/json
accept-encoding: gzip, deflate, br
accept-language: en-GB,en;q=0.9,en-US;q=0.8,de;q=0.7
origin: http://localhost:63342
referer: http://localhost:63342/
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="102", "Microsoft Edge";v="102"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "macOS"
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: cross-site
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44

Like I mentioned, I use a caddy reverse proxy, but that should not matter, because the it also doesn't work if I use a direct connection. If I let caddy add an Access-Control-Allow-Origin * header I still get the 406 (but not the CORS violation). But when I try postman or the browser access bar for a direct get to the nominatim API the header is already present, so that can't be the problem.

jakern commented 1 year ago

Hi, is your Nominatim instance still served by Apache behind your reverse proxy?

I encountered a similar issue today and solved it by adding AddType application/json .php to the Apache config.

It seems to be an issue noted in the Nominatim Docker repo already https://github.com/mediagis/nominatim-docker/issues/133

oldwired commented 1 year ago

I encountered a similar issue today and solved it by adding AddType application/json .php to the Apache config.

Thanks, works for me. :)

rooton commented 1 year ago

Unfortunately not work for me. The same error 406.

oldwired commented 1 year ago

@rooton have you checked the issue https://github.com/mediagis/nominatim-docker/issues/133 ? Looks like it's a problem with the nominatim-docker package

rooton commented 1 year ago

@oldwired Yes, thats definitely issue with nominatim-docker package. Firstly I have added AddType application/json .php on top of AddType text/html .php and it didn't work, but after moving application/json under text/html it finally worked.

arshiay commented 1 year ago

@oldwired Yes, thats definitely issue with nominatim-docker package. Firstly I have added AddType application/json .php on top of AddType text/html .php and it didn't work, but after moving application/json under text/html it finally worked.

hi where you add these line can you explain to me