How to call rest api in javascript
Alıntı:
https://levelup.gitconnected.com/all-possible-ways-of-making-an-api-call-in-plain-javascript-c0dee3c11b8b
Here are the possible ways to make an API call:
* XMLHttpRequest
* fetch
* Axios
* jQuery
* XMLHttpRequest
ES 6 ortaya çıkmadan önce, JavaScript'te HTTP isteği yapmanın tek yolu XMLHttpRequest idi. JavaScript'te HTTP istekleri yapmamızı sağlayan yerleşik bir tarayıcı nesnesidir.
let request = new XMLHttpRequest();
request.open("GET","https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY");
request.send();
request.onload() = () => {
console.log(request);
if(request.status ==200){
console.log(JSON.parse(request.response));
} else {
console.log('error ${request.status} ${request.statusText}');
}
}
Fetch
Fetch allows you to make an HTTP request in a similar manner as XMLHttpRequest but with a straightforward interface by using promises.
It’s not supported by old browsers (can be polyfilled), but very well supported among the modern ones. We can make an API call by using fetch in two ways.
Method 1:
fetch('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
.then(response => {
return response.jason();
});
By using Async and Await
async function getUsers(){
let response = await fetch('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY');
let data = await response.jason();
return data;
}
getUsers().then(data => console.log(data));
he fetch API is very powerful. We can easily send AJAX requests using the browser fetch API. The major disadvantage of fetch API is error handling.
Axios
Axios is an open source library for making HTTP requests and provides many great features, and it works both in browsers and Node.js. It is a promise-based HTTP client that can be used in plain JavaScript and advanced frameworks like React, Vue.js, and Angular.
It supports all modern browsers, including support for IE 8 and higher.
Installation:
If you are using any one of the package manager like npm or yarn.
npm install axios
or
yarn add axios
And include it in html file like this
<script src="./node_modules/axios/dist/axios.min.js"></script>
The easiest way to include Axios is by using external CDN:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
Now you can start sending HTTP request by including the following script in your HTML file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name ="viewport" content ="width-device-width, initial-scala = 1.0">
<meta http-equiv="X-UA-Compatible" content ="ie=edge">
<title> Documnet </title>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
axios.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
.then(response =>{ console.log(response.data)})
.catch(error => console.error(error));
</script>
</head>
<body>
</body>
</html>
The following are the advantages of Axios
Axios performs automatic transformations and returns the data in JSON format.
Better error handling
Axios has a wide range of supported browsers.
jQuery
jQuery has many methods to handle asynchronous HTTP requests. In order to use jQuery we need to include source file of jQuery and $.ajax() method is used to make HTTP request.
$.ajax()
<!DOCTYPE html>
<html lang="en">
<head>
<title> AJAX Api call by Jquery </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(documnet).ready( function(){
$.ajax({
url : "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY",
type : "GET",
success : function(){
console.log(result);
},
error : fuction(){
console.log(error);
}
})
})
</script>
</head>
<body>
</body>
</html>
The $.ajax method takes many parameters, some of which are required and others optional.
It contains two callback functions success and error to handle the response received.
Conclusion
Most of the real-time applications are using Axios to make HTTP requests. Axios is very easy and an open source library for making HTTP requests.
I have covered the most popular ways to make HTTP requests in JavaScript.
For your convenience, I am adding source code here .
No one’s perfect. Please comment any suggestions and enhancements.
Thanks for your time.
https://levelup.gitconnected.com/all-possible-ways-of-making-an-api-call-in-plain-javascript-c0dee3c11b8b
Alıntı:
https://levelup.gitconnected.com/all-possible-ways-of-making-an-api-call-in-plain-javascript-c0dee3c11b8b
https://levelup.gitconnected.com/all-possible-ways-of-making-an-api-call-in-plain-javascript-c0dee3c11b8b
Here are the possible ways to make an API call:
* XMLHttpRequest
* fetch
* Axios
* jQuery
* XMLHttpRequest
ES 6 ortaya çıkmadan önce, JavaScript'te HTTP isteği yapmanın tek yolu XMLHttpRequest idi. JavaScript'te HTTP istekleri yapmamızı sağlayan yerleşik bir tarayıcı nesnesidir.
let request = new XMLHttpRequest();
request.open("GET","https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY");
request.send();
request.onload() = () => {
console.log(request);
if(request.status ==200){
console.log(JSON.parse(request.response));
} else {
console.log('error ${request.status} ${request.statusText}');
}
}
Fetch
Fetch allows you to make an HTTP request in a similar manner as XMLHttpRequest but with a straightforward interface by using promises.
It’s not supported by old browsers (can be polyfilled), but very well supported among the modern ones. We can make an API call by using fetch in two ways.
Method 1:
fetch('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
.then(response => {
return response.jason();
});
By using Async and Await
async function getUsers(){
let response = await fetch('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY');
let data = await response.jason();
return data;
}
getUsers().then(data => console.log(data));
he fetch API is very powerful. We can easily send AJAX requests using the browser fetch API. The major disadvantage of fetch API is error handling.
Axios
Axios is an open source library for making HTTP requests and provides many great features, and it works both in browsers and Node.js. It is a promise-based HTTP client that can be used in plain JavaScript and advanced frameworks like React, Vue.js, and Angular.
It supports all modern browsers, including support for IE 8 and higher.
Installation:
If you are using any one of the package manager like npm or yarn.
npm install axios
or
yarn add axios
And include it in html file like this
<script src="./node_modules/axios/dist/axios.min.js"></script>
The easiest way to include Axios is by using external CDN:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
Now you can start sending HTTP request by including the following script in your HTML file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name ="viewport" content ="width-device-width, initial-scala = 1.0">
<meta http-equiv="X-UA-Compatible" content ="ie=edge">
<title> Documnet </title>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
axios.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
.then(response =>{ console.log(response.data)})
.catch(error => console.error(error));
</script>
</head>
<body>
</body>
</html>
The following are the advantages of Axios
Axios performs automatic transformations and returns the data in JSON format.
Better error handling
Axios has a wide range of supported browsers.
jQuery
jQuery has many methods to handle asynchronous HTTP requests. In order to use jQuery we need to include source file of jQuery and $.ajax() method is used to make HTTP request.
$.ajax()
<!DOCTYPE html>
<html lang="en">
<head>
<title> AJAX Api call by Jquery </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(documnet).ready( function(){
$.ajax({
url : "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY",
type : "GET",
success : function(){
console.log(result);
},
error : fuction(){
console.log(error);
}
})
})
</script>
</head>
<body>
</body>
</html>
The $.ajax method takes many parameters, some of which are required and others optional.
It contains two callback functions success and error to handle the response received.
Conclusion
Most of the real-time applications are using Axios to make HTTP requests. Axios is very easy and an open source library for making HTTP requests.
I have covered the most popular ways to make HTTP requests in JavaScript.
For your convenience, I am adding source code here .
No one’s perfect. Please comment any suggestions and enhancements.
Thanks for your time.
https://levelup.gitconnected.com/all-possible-ways-of-making-an-api-call-in-plain-javascript-c0dee3c11b8b
Alıntı:
https://levelup.gitconnected.com/all-possible-ways-of-making-an-api-call-in-plain-javascript-c0dee3c11b8b
Yorumlar
Yorum Gönder