1,fetch形式
let id = '123'
let urlGet = 'https://www.baidu.com?id=' + id
fetch(urlGet)
.then(response => {
if (!response.ok) {
throw new Error('请求失败: ' + response.status);
}
return response.json();
})
.then(data => {
console.log('Success:', data);
})
.catch(error => console.error('发生错误:', error));
2,xhr形式
let chapterId = 1;
let tpcId = 22;
let urlGet = 'https://www.baidu.com/?id=' + chapterId + '&tpcId=' + tpcId;
let xhr = new XMLHttpRequest();
xhr.open('GET', urlGet, true);
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
let data = JSON.parse(xhr.responseText);
console.log('Success:', data);
} else {
console.error('请求失败: ' + xhr.status);
}
};
xhr.onerror = function() {
console.error('发生错误');
};
xhr.send();