2014-03-06 30 views
1

我正在制作仪表板并从SoundCloud API中提取一些信息。我对这个API相对比较陌生,并且我能够将每个音轨的SoundCloud Widget列出。我的问题是,每次刷新时,我都会退出(我认为)。即使用户例如刷新页面,会话仍然存在吗?这是我现在所拥有的:JS:如何防止在SoundCloud API中登录后每次刷新丢失会话

function connectToSoundCloud(){ 
     //INTIALIZE AUTH WITH SC 
     SC.initialize({ 
     client_id: sc_client_id, 
     redirect_uri: sc_redirect_uri 
    }); 
     //INITIATE AUTH POPUP 
      SC.connect(function() { 
    SC.get('/me', function(me) { 

     if (me.username) { 
      document.getElementById('btn-disconnectFromSoundCloud').style.display=""; 
      document.getElementById('btn-connectToSoundCloud').style.display="none"; 
     } 

     $.ajax({ 
      type: "GET", 
      url: "http://api.soundcloud.com/tracks.json?client_id="+sc_client_id+"", 
      error: function(result, status){ 
      }, 
      success: function(result, status){ 
         //CODE TO DISPLAY TRACKS HERE 
      } 
     }); 
    }); 
}); 
    } 

任何帮助表示赞赏。干杯。

回答

0

你凑了jStorage?从here下载它。将jStorage.js文件导入到您的代码中,并且您很好。 <script src="YOUR_FOLDER/jStorage.js"></script> 然后你可以存储登录的用户是这样的:每当你想获取有关登录的用户信息

SC.connect(function() { 
SC.get('/me', function(me) { 
    $.jStorage.set('userInfo',me); // store the user to storage 
    console.log("Logged in userId: "+ me.id); 
    console.log('Logged in username: '+me.username); 
    location.reload(); // reloads the page 
    }); 

}); 

然后,只需使用:

var loggedInUser = $.jStorage.get('userInfo');

要退出,只是使用

$.jStorage.deleteKey('userInfo'); // delete the logged in user 
location.reload(); // reloads the page 
+0

这样可以保存正在发送的用户数据,但这样做是否也确保它们已登录?是否仍然上传和评论功能? – theptrk