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

本文是小编为大家收集整理的关于如何用node js将我的json发送到我的视图上?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我之前有一个关于这个的问题,所以看看 这里.

我现在可以登录到 spotify 并获取已登录用户的播放列表.我现在想将这些播放列表(它们以 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);"的问题在于我将其作为 url: localhost:8888/playlists 而在此页面中我只看到 json.我想将它发送到一个视图,在那里我可以选择我显示的信息.有人可以帮我解决这个问题吗?

提前致谢

推荐答案

在这种情况下,您可以使用您选择的库向服务器发出 ajax 请求,当您收到 ajax 后的数据时,您更新您的使用数据查看.使用 jQuery,您可能会执行以下操作:

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

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

本文地址:https://www.itbaoku.cn/post/1937780.html

问题描述

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

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?

Thanks in advance

推荐答案

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

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