Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Returning coordinates via AJAX

I'm trying to return value via AJAX. Below is the code that I put together based on some examples I found and it seems to work, but I think there's a better way of doing it?

(async () => {
    const url = new URL("/ajax-page", window.location.origin);
            
    const headers = new Headers([
        [
            "Content-Type",
            "application/x-www-form-urlencoded; charset=UTF-8"
        ]
    ]);

    const body = new URLSearchParams([
        ["sl", slug]
    ]);
    
    const res = await fetch(url, {
        method: "POST",
        headers,
        body
    });
                        
    if (res.ok) {                                
        let mkr = await res.text();
            crd = mkr.split("|");  
            lat = crd[0],
            lon = crd[1];

          console.log(lat+' '+lon);
    }
})();

PHP (ajax-page)

// php magic gets values
echo  $mar['lat'].'|'.$mar['lon'];

Do I even need to use (async () => {...})(); anymore?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

These lines:

lat = crd[0],
lon = crd[1];

are not variable declarations (because there's no let keyword). It's using the comma operator, so it's equivalent to:

lat = (crd[0], lon = crd[1])

The comma operator returns the value of the second expression, so both lat and lon are being set to crd[1].

The cause of this problem is that you ended the previous two lines with ; rather than ,. The whole declaration should be:

        let mkr = await res.text(),
            crd = mkr.split("|"),
            lat = crd[0],
            lon = crd[1];

If you use your IDE's auto-indent feature it would have shown that this isn't grouped the way you intended.

about 4 years ago · Juan Pablo Isaza Relatório

0

await can be used only inside an async function. You cannot avoid it. Asynchronous functions implicitly return a promise, so other async functins can await their results.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda