ooooohyesssss
ooooohyesssss

Reputation: 139

What is the best way to send a GET request to the server in vanilla JavaScript?

What is the best way to send a GET request to the server in vanilla JavaScript?

Upvotes: 7

Views: 19467

Answers (7)

S. Ber Rosenberg
S. Ber Rosenberg

Reputation: 107

if you get installt PHP you can use the get_file_content var

 <html>
<script>
var date= "<?php 
echo(file_get_contents('https://apizmanim.com/twilio/zipapi.php?11211?2021/05/14')?>";
document.write(date);
</script>
</html>

Upvotes: -1

Mikel Wohlschlegel
Mikel Wohlschlegel

Reputation: 1476

I'm not sure if we can claim here a "best way", but you can use

XMLHttpRequest

or if you want to use a library

Axios

Upvotes: 0

Gatien Anizan
Gatien Anizan

Reputation: 130

In vanilla javascript, you can use the fetch API.

fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((myJson) => {
    console.log(myJson);
  });

Upvotes: 13

Syll
Syll

Reputation: 41

Using the XMLHttpRequest (XHR) Object.

Code example:

const http = new XMLHttpRequest();
const url='/test';
http.open("GET", url);
http.send();

http.onreadystatechange = (e) => {
  console.log('done')
}

Upvotes: 1

Saul Ramirez
Saul Ramirez

Reputation: 426

You can try with Fetch

function request() {
            fetch('http://example.com/movies.json')
                .then(function(response) {
                    console.log(response.json())
                })
                .then(function(myJson) {
                    console.log(myJson);
                });
        }

        request()

Upvotes: 0

damien guesdon
damien guesdon

Reputation: 31

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", THE_URL, false ); // false for synchronous request
    xmlHttp.send( null );
    return xmlHttp.responseText;

Upvotes: 0

jerkan
jerkan

Reputation: 695

You can do a redirection to do a synchronous GET request:

var url = 'http://domain/path/?var1=&var2=';

window.location = url;

Upvotes: 2

Related Questions