Reputation: 139
What is the best way to send a GET request to the server in vanilla JavaScript?
Upvotes: 7
Views: 19467
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
Reputation: 1476
I'm not sure if we can claim here a "best way", but you can use
or if you want to use a library
Upvotes: 0
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
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
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
Reputation: 31
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", THE_URL, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
Upvotes: 0
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