发布JSON数据以呈现页面 [英] Post JSON data to render page

查看:110
本文介绍了发布JSON数据以呈现页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从URL:/ accounts /:id / users获得我的搜索结果为JSON格式,我需要检索该JSON数据以呈现/ contacts / find。 (后一页显示搜索结果。)



这是我的应用路由器。有了这个,我可以在JSON格式中看到/ contacts / find中的搜索结果:

  app.post('/ contacts / find',function(req,res){
var searchStr = req.param('searchStr',null);
if(null == searchStr){
res.send(400 );
return;
}

models.Account.findByString(searchStr,function onSearchDone(err,accounts){
if(err || accounts.length = = 0){
res.send(404);
} else {
res.send(accounts);
}
});
} );

如何使用帐户(采用JSON格式)并使用帐户重新呈现网页信息?

  app.get('/ contacts / find',函数(req,res){
res.render('searchResult.ejs',{
accounts:req.accounts,
email:req.accounts.email
})
}

app.get方法不起作用,URL只显示JSON数据。 / p>

解决方案所以 accounts 是一个JSON字符串吗?在这种情况下,您可以解析它(将其转换回对象)并将结果传递给您的模板: req.accounts);
res.render('searchResult.ejs',{
accounts:accounts,
email:accounts.email
})


I am getting my search result as JSON format from the URL: "/accounts/:id/users" and I need to retrieve that JSON data to render "/contacts/find". (The latter page shows search results.)

Here is my app router. With this I could see the result of the search in /contacts/find in JSON format:

app.post('/contacts/find', function(req, res) {
  var searchStr = req.param('searchStr', null);
  if ( null == searchStr ) {
    res.send(400);
    return;
  }

  models.Account.findByString(searchStr, function onSearchDone(err,accounts) {
    if (err || accounts.length == 0) {
      res.send(404);
    } else {
      res.send(accounts);
    }
  });
});

How can I use "accounts" (in JSON format) and re-render the page with the account info? I am trying with the below code snippet, but it does not work.

app.get('/contacts/find', function(req, res) {
 res.render('searchResult.ejs', {
  accounts: req.accounts,
  email: req.accounts.email
 })
}

The app.get method is not working. The URL just shows JSON data.

解决方案

So accounts is a JSON string? In that case, you can parse it (to convert it back to an object) and pass the result to your template:

var accounts = JSON.parse(req.accounts);
res.render('searchResult.ejs', {
  accounts : accounts,
  email    : accounts.email
 })

这篇关于发布JSON数据以呈现页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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