浏览器如何发起 GET 请求 - 详细指南与示例
浏览器发起 GET 请求的方法:
-
在浏览器的地址栏中输入请求的 URL 地址,按下回车键即可发起 GET 请求。
-
在页面中使用超链接 ('') 或表单 ('
-
在浏览器的开发者工具中使用 JavaScript 代码发起 GET 请求,例如使用 'XMLHttpRequest' 对象或 'fetch' 函数。
示例代码:
使用超链接:
<a href='https://example.com'>点击跳转到 example.com</a>
使用表单:
<form action='https://example.com' method='get'>
<input type='text' name='q' placeholder='搜索'>
<button type='submit'>搜索</button>
</form>
使用 JavaScript:
// 使用 XMLHttpRequest 对象发起 GET 请求
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com');
xhr.send();
// 使用 fetch 函数发起 GET 请求
fetch('https://example.com')
.then(response => response.text())
.then(data => console.log(data))
.catch(error => console.error(error));
原文地址: https://www.cveoy.top/t/topic/j52U 著作权归作者所有。请勿转载和采集!