在HTML页面中,可以使用JavaScript中的fetch函数来发送HTTP请求并获取数据。fetch函数返回一个Promise对象,可以使用then方法处理返回的数据。以下是一个使用fetch函数获取数据的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Fetch Example</title>
</head>
<body>
<script>
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
// 处理获取到的数据
console.log(data);
})
.catch(error => {
// 处理错误
console.error(error);
});
</script>
</body>
</html>
```
上述代码使用fetch函数发送一个GET请求,并通过then方法处理返回的响应。在第一个then方法中,使用json方法将响应数据解析为JSON格式。然后在第二个then方法中,使用解析后的数据进行处理。如果发生错误,可以通过catch方法捕获并进行处理。 请注意,在使用fetch函数发送请求时,需要注意跨域资源共享(CORS)的限制。