2016-11-17 26 views
0

我有一個關於此的前一個問題,所以看看here如何發送我的JSON到節點js的我的視圖

我現在可以登錄到Spotify的,並得到了在loged用戶的播放列表。我現在想送這些播放列表(它們是JSON),以有觀點認爲,一個HTML頁面或...

這是我的代碼如下所示:

app.get('/playlists', function(req, res) { 
    var state = generateRandomString(16); 
    res.cookie(stateKey, state); 
    var scope = 'playlist-read-private'; 
    var options = { 
    url:"https://api.spotify.com/v1/me/playlists", 
    headers:{ 
     'Authorization': "Bearer " + token, 
     'response_type': 'code', 
     'client_id': client_id, 
     'scope': scope, 
     'state': state 
    }, 
    json:true 
    }; 
    request.get(options, function(error, req, body){ 
    console.log(body); 
    //Here I want to send the json to the view 
    res.send(body); 
    }); 
}); 

與此代碼「res.send(body);」問題是,我得到這個作爲網址:本地主機:8888 /播放列表,並在此頁面中我只看到了JSON。我想將它發送到我可以選擇顯示的信息的視圖。有人可以幫我弄這個嗎?提前

感謝

+0

@aring這是我的新問題 –

+0

什麼是「視圖」,您是否使用視圖引擎,如Jade,EJS等? – adeneo

+0

我發現了一些信息來做到這一點與ejs,但不知道如何使用它 –

回答

1

在這種情況下,你可以使用一個Ajax請求到服務器與您選擇的圖書館和當你在ajax之後接收到數據時,你使用數據更新你的視圖。使用jQuery你可以做這樣的事情:

var jqxhr = $.get('/playlist', {parameters}); 
jqxhr.done(function(data){ 
//data is your json response you can manipulate. 
}); 

您可能還需要使用res.json()女巫更適合送快遞JSON響應。

+0

謝謝,我會試試這個 –

1

如果你想送一個JSON,你可以做到這一點

return res.status(200).json({ "text": "Hello World!" }); 
+0

是的,這是有效的,但這不是一種方式將其發送到我喜歡HTML的頁面。並在該頁面上,我想顯示播放列表,但不是在json中。 –

相關問題