我想使用javascript使用lastfm api进行一些测试:
这不起作用,也不知道为什么……我通过了身份验证并获得了令牌密钥。 因此,默认情况下,它必须获取我的用户信息...
/*his code is commented to get the token of my user
var url = window.location.href; // or window.location.href for current url
var captured = /token=([^&]+)/.exec(url)[1]; // Value is in [1] ('384' in our case)
var result = captured ? captured : 'myDefaultValue';
console.log(captured);
sessionStorage.setItem("mytoken",captured);
*/
var myAPI_key="b6720a4ef50c0a1f63419e334fbf9c74";
//TAke data user from https://www.last.fm/api/show/user.getInfo
//eXAMPLE URL: http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=rj&api_key=YOUR_API_KEY&format=json
//mYurl: http://ws.audioscrobbler.com/2.0/?method=user.getinfo&api_key=b6720a4ef50c0a1f63419e334fbf9c74&format=json
$.ajax({
type : 'GET',
url : 'http://ws.audioscrobbler.com/2.0/',
data : 'method=user.getinfo&' +
'api_key=b6720a4ef50c0a1f63419e334fbf9c74&' +
'format=json',
dataType : 'json',
success : function(data) {
$('#success #userName').html(data.user.name);
$('#success #userImage').html('<img src="' + data.user.image['#text'] + '" />');
$('#success #userPlaycount').html(data.user.playcount);
},
error : function(code, message){
$('#error').html('Error Code: ' + code + ', Error Message: ' + message);
}
});
<!DOCTYPE html>
<html>
<head>
<title>Menu principal Last FM</title>
<meta charset="utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<!-- <script type="text/javascript" src="./js/constants.js"></script>-->
<script type="text/javascript" src="./js/main.js"></script>
</head>
<body>
<h1>Im login to</h1>
<div id="userData">
</div>
<div id="success">
<div id="userName"></div>
<div id="userImage"></div>
<div id="userPlaycount"></div>
</div>
<div id="error"></div>
</body>
</html>
还可以使用httpRequest进行呼叫
这是出于教育目的。
谢谢