如何通过节点js将我的json发送到我的视图

时间:2022-12-01 15:58:49

I have a previous question about this so take a look here.

我之前有一个关于此的问题,请看这里。

I can now login to spotify and get the playlists of the user that is loged in. I now want to send these playlists (they are in json) to a view of to an html page or ...

我现在可以登录到spotify并获取已插入的用户的播放列表。我现在想要将这些播放列表(它们在json中)发送到html页面的视图或...

This is what my code looks like:

这就是我的代码:

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);
  });
});

The problem with this code "res.send(body);" is that I get this as url: localhost:8888/playlists and in this page I just see the json. I would like to send it to a view where I can chose what info I show. Can someone help me with this?

这段代码的问题“res.send(body);”我得到的是url:localhost:8888 /播放列表,在这个页面中我只看到了json。我想把它发送到一个视图,我可以选择我显示的信息。有人可以帮我弄这个吗?

Thanks in advance

提前致谢

2 个解决方案

#1


1  

In that case, you can use an ajax request to the server with a library of your choice, and when you receive the data after the ajax, you update your view using the data. With jQuery you might do something like:

在这种情况下,您可以使用您选择的库对服务器使用ajax请求,当您在ajax之后收到数据时,可以使用数据更新视图。使用jQuery,您可能会执行以下操作:

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

You might also want to use res.json() witch is more appropriate to send json responses in express.

您可能还想使用res.json()女巫更适合在express中发送json响应。

#2


1  

If you want to send a JSON you can do this

如果要发送JSON,可以执行此操作

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

#1


1  

In that case, you can use an ajax request to the server with a library of your choice, and when you receive the data after the ajax, you update your view using the data. With jQuery you might do something like:

在这种情况下,您可以使用您选择的库对服务器使用ajax请求,当您在ajax之后收到数据时,可以使用数据更新视图。使用jQuery,您可能会执行以下操作:

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

You might also want to use res.json() witch is more appropriate to send json responses in express.

您可能还想使用res.json()女巫更适合在express中发送json响应。

#2


1  

If you want to send a JSON you can do this

如果要发送JSON,可以执行此操作

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