如何使用Node JS将JSON发送到视图 [英] How can I send my json to my view with node js

查看:64
本文介绍了如何使用Node JS将JSON发送到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前对此有疑问,因此请查看

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

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

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 ...

这是我的代码的样子:

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);"的问题是我将其作为url获得:localhost:8888/playlists,在此页面中,我仅看到json.我想将其发送到一个视图,在这里我可以选择显示的信息.有人可以帮我吗?

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?

预先感谢

推荐答案

在这种情况下,您可以将ajax请求与您选择的库一起使用到服务器,并且在ajax之后收到数据时,您将更新查看使用数据.使用jQuery,您可能会做类似的事情:

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:

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

您可能还想使用res.json(),更适合以快递方式发送json响应.

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

这篇关于如何使用Node JS将JSON发送到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆