您好,欢迎访问一九零五行业门户网

如何使用 REST API 创建 Covid19 国家/地区状态项目?

在创建项目之前,我们将首先讨论 rest api。 rest 是一种软件架构风格和有助于制作在线服务的标准集合。表述性状态传输(representational state transfer)的全称是rest。同时,应用程序编程接口(api)允许两个或多个计算机程序之间进行通信。它是一个为其他软件程序提供服务的软件接口。用户必须遵循基于 http(超文本传输​​协议)的 rest api 规则来访问 web 服务。
传统的 http 方法(如 get、post、put 和 delete)访问和修改 rest api 中的数据对象等资源。这些由 uri(统一资源标识符)标识。可以使用 api 以多种格式传送数据,包括 xml 和 json。可以使用 rest 构建小型、快速且易于扩展的 web 服务。它是为了与万维网的 http 协议进行通信而开发的。由于其以标准协议为基础,rest api 可供各种客户端使用,包括 web 浏览器、移动应用程序和其他服务器。
rest api 经常用于 web 和移动应用程序开发,因为它为应用程序提供了一种简单且标准化的方法来访问和更改服务器上的资源。
创建 covid19 国家明智状态项目的步骤使用 rest api,人们可以按照以下基本步骤构建一个 covid-19 国家/地区状态项目 -
第 1 步 - 研究可靠的 api,提供按国家/地区细分的 covid-19 数据。在本教程中,我们使用以下 api 链接:https://covid19api.com/。
步骤 2 - 请参阅 api 文档,了解如何获取数据以及可按国家/地区过滤数据的参数。
步骤 3 - 使用 ajax 方法,在 api 上发送 http 请求并获取响应数据。
步骤 4 - 为了开发项目的前端(以用户友好的方式呈现数据),我们使用 html 表格和 css 来更好地直观地表示数据。
covid19 国家明智状况项目在这里,我们将构建实际的项目。它将分为三部分:执行 http 请求的 javascript ajax 代码、显示内容的 html 正文以及使其用户友好的 css 样式。我们使用 jquery ajax 库来使代码更易于用户阅读和使用。
http 响应正文 在详细了解代表国家/地区 covid19 状态的实际 html 正文代码之前,我们需要查看 api 响应并了解其结构。
以下是我们收到的 api 响应的一部分 -
{ id: 027ce495-cf80-48da-afb7-6b8f95b12a01, message: , global: { newconfirmed: 208060, totalconfirmed: 671410179, newdeaths: 2047, totaldeaths: 6771936, newrecovered: 0, totalrecovered: 0, date: 2023-02-18t04:36:09.159z }, countries: [ { id: 2390f7cb-1c24-4164-bfc3-688afed8bbe7, country: afghanistan, countrycode: af, slug: afghanistan, newconfirmed: 16, totalconfirmed: 209072, newdeaths: 0, totaldeaths: 7896, newrecovered: 0, totalrecovered: 0, date: 2023-02-18t04:36:09.159z, premium: {} }, { id: 8591babe-97a3-44f5-8e38-06df8ae67a55, country: albania, countrycode: al, slug: albania, newconfirmed: 9, totalconfirmed: 334273, newdeaths: 0, totaldeaths: 3596, newrecovered: 0, totalrecovered: 0, date: 2023-02-18t04:36:09.159z, premium: {} }, ... ] date: 2023-02-18t04:36:09.159z}
在此回复中,我们提供了有关新冠病毒国家/地区的一些详细信息,但该项目的重要部分是“国家/地区”键。它包含一组对象,表示特定国家/地区的 covid19 国家/地区详细信息。该对象的键是不言自明的,例如“country”包含国家/地区名称。 “newconfirmed”存储新确诊的 covid19 病例。 “totalconfirmed”存储该国家/地区的确诊病例总数。 “newdeaths”代表最近的死亡人数。 “totaldeaths”是指该国的死亡总人数,“newrecovered”代表当前康复患者,“totalrecovered”代表康复患者总数。
示例<html><head> <script src=https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js></script> <title> covid19 country wise status project </title> <style> .text-center { text-align: center; } #mytable { border-collapse: collapse; width: 100%; } #mytable td, #mytable th { border: 1px solid #ddd; padding: 8px; } #mytable tr:nth-child(even) { background-color: #f2f2f2; } #mytable th { padding-top: 12px; padding-bottom: 12px; text-align: left; background-color: #008b86; color: white; } </style></head><body> <h2 class=text-center> covid19 country wise status project </h2> <!-- table --> <table id=mytable> <thead> <th> country name </th> <th> new confirmed </th> <th> new deaths </th> <th> new recovered </th> <th> total confirmed </th> <th> total deaths </th> <th> total recovered </th> </thead> </table> <script> let mytable = document.getelementbyid('mytable') // ajax http request $.ajax({ url: 'https://api.covid19api.com/summary', type: 'get', success: function (response) { let data = response.countries console.log(data) let element = '' data.foreach((country) => { element += '<tr><td>' + country.country + '</td>' + '<td>' + country.newconfirmed + '</td>' + '<td>' + country.newdeaths + '</td>' + '<td>' + country.newrecovered + '</td>' + '<td>' + country.totalconfirmed + '</td>' + '<td>' + country.totaldeaths + '</td>' + '<td>' + country.totalrecovered + '</td></tr>' }) mytable.innerhtml += element }, }) </script> </body></html>
该项目将帮助初学者了解有关 ajax、javascript、html 和 css 的更多信息。它还可以用作多个县的新冠病毒状态快速检查。
以上就是如何使用 rest api 创建 covid19 国家/地区状态项目?的详细内容。
其它类似信息

推荐信息