从节点服务器Spotify

时间:2016-11-24 11:03:28

标签: javascript jquery json node.js spotify

我想将正确格式化的JSON从节点服务器发送回前端。

var express = require('express');
var app = express();
app.get('/route', function(req, res){
    res.json(tracks)
});

然后在前端,我正在使用jQuery发出GET请求

$.get('http://localhost:8888/route', function(data){
    console.log(JSON.parse(data));
});

这是返回数据,但是它将它作为字符串返回。我曾尝试使用JSON.parse,但收到以下错误消息。节点侧的data是一个对象数组。也许这会引起一个问题。

  

未捕获的SyntaxError:意外的令牌,位于位置2847(...)的JSON中

要填充轨道数组,我有以下代码

tracks = [];
for (var i = 0; i < songLinks.length; i++) {
  request('https://api.spotify.com/v1/tracks/' + songLinks[i].split(":").pop(), function(error, response, body) {
    tracks.push(body)
  });
}

曲目数组中的第一项是:

  

“{↵”album“:{↵”album_type“:”专辑“,↵”艺术家“:[{↵
  “external_urls”:{↵“spotify”:   “https://open.spotify.com/artist/1yAwtBaoHLEDWAnWR87hBT”↵},↵
  “href”:“https://api.spotify.com/v1/artists/1yAwtBaoHLEDWAnWR87hBT”,↵   “id”:“1yAwtBaoHLEDWAnWR87hBT”,↵“名称”:“谦虚鼠标”,↵
  “type”:“artist”,↵“uri”:   “spotify:artist:1yAwtBaoHLEDWAnWR87hBT”↵}],↵
  “available_markets”:[“AD”,“AR”,“AT”,“AU”,“BE”,“BG”,“BO”,   “BR”,“CA”,“CH”,“CL”,“CO”,“CR”,“CY”,“CZ”,“DE”,“DK”,“DO”,   “EC”,“EE”,“ES”,“FI”,“FR”,“GB”,“GR”,“GT”,“HK”,“HN”,“HU”,   “ID”,“IE”,“IS”,“IT”,“JP”,“LI”,“LT”,“LU”,“LV”,“MC”,“MT”,   “MX”,“MY”,“NI”,“NL”,“NO”,“NZ”,“PA”,“PE”,“PH”,“PL”,“PT”,   “PY”,“SE”,“SG”,“SK”,“SV”,“TR”,“TW”,“US”,“UY”],↵
  “external_urls”:{↵“spotify”:   “https://open.spotify.com/album/4YvdAtWh6LlnIrv0qIqkCF”↵},↵
  “href”:“https://api.spotify.com/v1/albums/4YvdAtWh6LlnIrv0qIqkCF”,↵   “id”:“4YvdAtWh6LlnIrv0qIqkCF”,↵“images”:[{↵“height”:   640,↵“url”:   “https://i.scdn.co/image/411d254a0e46f509dda22f58d699782f16f7bd44”,↵
  “width”:640↵},{↵“height”:300,↵“url”:   “https://i.scdn.co/image/982b3b7c3ad3e81aed9e0475da07894262f93923”,↵
  “width”:300↵},{↵“height”:64,↵“url”:   “https://i.scdn.co/image/e4a25681c1163b349ff71b464d2cfc8fda089d58”,↵
  “宽度”:64↵}],↵“名称”:“没有人是第一个,你是下一个”,↵   “type”:“album”,↵“uri”:“spotify:album:4YvdAtWh6LlnIrv0qIqkCF”↵   },↵“艺术家”:[{↵“external_urls”:{↵“spotify”:   “https://open.spotify.com/artist/1yAwtBaoHLEDWAnWR87hBT”↵},↵
  “href”:“https://api.spotify.com/v1/artists/1yAwtBaoHLEDWAnWR87hBT”,↵   “id”:“1yAwtBaoHLEDWAnWR87hBT”,↵“名称”:“谦虚鼠标”,↵
  “type”:“artist”,↵“uri”:   “spotify:artist:1yAwtBaoHLEDWAnWR87hBT”↵}],↵“available_markets”:   [“AD”,“AR”,“AT”,“AU”,“BE”,“BG”,“BO”,“BR”,“CA”,“CH”,“CL”,   “CO”,“CR”,“CY”,“CZ”,“DE”,“DK”,“DO”,“EC”,“EE”,“ES”,“FI”,   “FR”,“GB”,“GR”,“GT”,“HK”,“HN”,“HU”,“ID”,“IE”,“IS”,“IT”,   “JP”,“LI”,“LT”,“LU”,“LV”,“MC”,“MT”,“MX”,“MY”,“NI”,“NL”,   “NO”,“NZ”,“PA”,“PE”,“PH”,“PL”,“PT”,“PY”,“SE”,“SG”,“SK”,   “SV”,“TR”,“TW”,“US”,“UY”],disc“disc_number”:1,↵“duration_ms”:   330573,↵“显式”:false,↵“external_ids”:{↵“isrc”:   “USSM10700655”↵},↵“external_urls”:{↵“spotify”:   “https://open.spotify.com/track/6Z3pIqEp5n5faSopwto0tB”↵},↵“href”   :“https://api.spotify.com/v1/tracks/6Z3pIqEp5n5faSopwto0tB”,↵“id”:   “6Z3pIqEp5n5faSopwto0tB”,↵“name”:“King Rat”,↵“人气”:38,↵   “preview_url”:   “https://p.scdn.co/mp3-preview/037294fdd97c94bebf14e7ec1b1fb2c0a1986adc”,↵   “track_number”:7,type“type”:“track”,↵“uri”:   “Spotify的:轨道:6Z3pIqEp5n5faSopwto0tB” ↵}“

以下是回复的屏幕截图,以防万一我的问题更清晰。

enter image description here

1 个答案:

答案 0 :(得分:1)

将正文更改为回复

request('https://api.spotify.com/v1/tracks/' + songLinks[i].split(":").pop(), function(error, response, body) {
    tracks.push(JSON.parse(body));
  });