在没有jQuery的情况下,您可以使用原生JavaScript的Fetch API或XMLHttpRequest来获取和处理JSON数据。以下是两种方法的示例:
fetch('https://api.example.com/data.json')
.then(response => response.json())
.then(data => {
console.log(data);
// 处理JSON数据
})
.catch(error => {
console.error('Error fetching data:', error);
});
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.example.com/data.json', true);
xhr.responseType = 'json';
xhr.onload = function() {
if (xhr.status === 200) {
var data = xhr.response;
console.log(data);
// 处理JSON数据
} else {
console.error('Error fetching data:', xhr.statusText);
}
};
xhr.onerror = function() {
console.error('Error fetching data:', xhr.statusText);
};
xhr.send();
这两种方法都可以在没有jQuery的情况下获取和处理JSON数据。Fetch API是现代浏览器中的一种较新的方法,而XMLHttpRequest则是较旧的方法,但在较旧的浏览器中仍然可以使用。
推荐的腾讯云相关产品:
产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云