如何使用节点 js 将我的 json 发送到我的视图 [英] How can I send my json to my view with node js

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

问题描述

我之前有一个关于此的问题,请查看 此处.

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

我现在可以登录 Spotify 并获取已登录用户的播放列表.我现在想将这些播放列表(它们在 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() 更适合在 express 中发送 json 响应.

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

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

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