JavaScript is a very powerful programming language that can extend its functionality by calling APIs and web services. In this article, we'll dive into how to call interfaces using JavaScript.
1. Interface concept
Interface is the communication channel between different software systems, also called API (Application Programming Interface). Through APIs, programmers can even enable communication between different programming languages. So, if we want to use JavaScript to call interfaces, we need to understand the basic concepts of interfaces.
2. Calling method
JavaScript can call API in a variety of ways. The most commonly used methods are AJAX (Asynchronous JavaScript and XML) and Fetch API. Fetch API is a new API built into modern browsers that can send HTTP requests to the server side and receive responses.
3. Fetch API
Fetch API is a Promise-based API, so it is very suitable for execution in an asynchronous environment. Using the Fetch API, you can send an HTTP request and process the results of the response using the resolve and reject functions. For example:
fetch('https://api.example.com/data') .then(response => response.json()) .then(data => console.log(data)) .catch(err => console.error(err));
The above code sends a GET request to https://api.example.com/data and then parses the response JSON and outputs it to the browser console. If the request is successful, the then callback function will be triggered; if an error occurs, the catch callback function will be triggered.
4. AJAX
AJAX is a technology that interacts with the back-end server system and the front-end web page. It can achieve asynchronous updates without the need to update the data after the web page is fully loaded. This technology is based on the XMLHttpRequest object, which interacts with the server by sending HTTP requests, receives data from the server, and uses JavaScript to update the web page content without refreshing the entire page. For example:
const xhr = new XMLHttpRequest(); xhr.onload = function() { if (xhr.status === 200) { console.log(xhr.responseText); } else { console.error(xhr.statusText); } }; xhr.open('GET', 'https://api.example.com/data'); xhr.send();
The above code creates an XMLHttpRequest object, sends a GET request to https://api.example.com/data, and returns the response to the browser console. If the request is successful, the response text output will be responded; if the request fails, the status text output will be responded.
5. Basic interface call
Next, we will use JavaScript to call a sample API.
fetch('https://swapi.dev/api/people/1/') .then(response => response.json()) .then(data => console.log(data)) .catch(err => console.error(err));
The above code calls https://swapi.dev/api/people/1/, which returns information about a character in "Star Wars". If the response is successful, the then callback function will be triggered, the response JSON will be parsed and output to the browser console.
6. API authorization
Some APIs require authorization to access. Please be sure to obtain full authorization before calling the API. For example, many APIs use the OAuth 2.0 authentication framework to protect their resources. OAuth 2.0 is a popular open standard for accessing protected APIs, which allows users to authorize applications and access required resources. OAuth 2.0 allows four typical authorization processes: authorization code process, implicit process, password credential process and client credential process.
7. Summary
In this article, we took an in-depth look at how to use JavaScript to call APIs. We introduced the two methods of Fetch API and AJAX, and provided a basic interface call example. Interfaces are an essential part of modern web development, so understanding how to use JavaScript to call interfaces will help you develop more powerful web applications. At the same time, remember to obtain sufficient authorization before using the API.
The above is the detailed content of How to call interface with javascript. For more information, please follow other related articles on the PHP Chinese website!