Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!