示例代码:
// 使用JavaScript的XMLHttpRequest发送GET请求 var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://api.example.com/data'); xhr.send();
xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.status === 200) { console.log(xhr.responseText); } else { console.error('请求错误: ' + xhr.status); } } };
// 使用JavaScript的fetch发送GET请求 fetch('http://api.example.com/data', { method: 'GET', }).then(response => { if (response.ok) { return response.text(); } else { throw new Error('请求错误: ' + response.status); } }).then(data => { console.log(data); }).catch(error => { console.error(error); });