<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function getJSON(url){
return new Promise((resolve,rejrct)=>{
var xhr=new XMLHttpRequest();
xhr.open("get",url,true);
xhr.send();
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200){
try{
var response=JSON.parse(xhr.responseText);
resolve(response);
}catch(e){
reject(e);
}
}else{
rejrct(new Error(xhr.statusText))
}
}
}
})
}
getJSON("https://api.imjad.cn/cloudmusic/?type=song&id=32785674").then(resp=>console.log(resp))
</script>
</body>
</html>
promise封装ajax
最新推荐文章于 2024-03-13 11:16:45 发布
本文介绍如何使用JavaScript通过API获取云音乐的歌曲信息,以'https://api.imjad.cn/cloudmusic/?type=song&id=32785674'为例,展示了AJAX请求的实现和JSON数据解析过程。
摘要由CSDN通过智能技术生成