M
M
msdoc112022-02-08 10:10:24
JavaScript
msdoc11, 2022-02-08 10:10:24

How to ignore the preferred language of the system, browser when requesting an API?

Hello, there is a code that determines the city by coordinates

if ("geolocation" in navigator) {
            console.log('Доступно')
            navigator.geolocation.getCurrentPosition(function(position) {

                $.ajax({
                    url: "https://nominatim.openstreetmap.org/reverse",
                    data: {
                        lat: position.coords.latitude,
                        lon: position.coords.longitude,
                        format: "json"
                    },
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader(
                            'User-Agent',
                            'ID of your APP/service/website/etc. v0.1'
                        )
                    },
                    dataType: "json",
                    type: "GET",
                    async: true,
                    crossDomain: true
                }).done(function(res) {
                    if(document.querySelector('.city')){
                         document.querySelector('.city').innerHTML = res.address.city;
                    }
                   
                    console.log(res.address)
                    console.log(res.address.city)
                    
                }).fail(function(error) {
                    console.error(error)
                })

            });
         
        } else {
            console.log('Не доступно');
            document.querySelector('.city').innerHTML = 'Нет данных' ;
        }


If, for example, another language is installed on the phone - a region (for example, eng), then api returns to eng. Moscow.
How to change the code so that it is ignored and always returns to ru: Moscow, etc.?
The API is not strong.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2022-02-08
@msdoc11

RTFM.
https://nominatim.org/release-docs/develop/api/Reverse/

Language of results
accept-language=<browser language string>
Preferred language order for showing search results, overrides the value specified in the "Accept-Language" HTTP header. Either use a standard RFC2616 accept-language string or a simple comma-separated list of language codes.

V
Vladimir Korotenko, 2022-02-08
@firedragon

Or look at the API documentation, most likely there is a language parameter.
Or, as you have already been told, set the header with the language.
Option 1 is more correct, I think the second is more like a crutch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question