// 创建XMLHttpRequest对象 var xhr = new XMLHttpRequest(); // 设置请求方法和请求地址 xhr.open('GET', 'https://api.example.com/api-endpoint'); // 设置请求头 xhr.setRequestHeader('Content-Type', 'application/json'); // 发送请求 xhr.send(); // 处理请求的响应 xhr.onreadystatechange = function() { // 当请求完成时 if (xhr.readyState === XMLHttpRequest.DONE) { // 当请求成功时 if (xhr.status === 200) { // 处理响应数据 var response = JSON.parse(xhr.responseText); console.log(response); } else { // 请求失败时 console.error('请求失败'); } } };
以上代码使用XMLHttpRequest对象发送一个GET请求到https://api.example.com/api-endpoint,并处理响应数据。请根据实际情况修改请求方法、请求地址、请求头和响应处理部分的代码。