如何将 json 响应传递给 jade 并循环结果

How to pass json response to jade and loop the results

我正在尝试使用 node.js 应用程序,我正在发出 post http 请求。响应是 json 格式,我需要在 jade 页面中显示它。当我尝试这样做时,我没有得到正确的 JSON 响应。

app.js

var request = require('request');
var options = {
    url: 'my URL goes here ',
    method: 'POST',
    headers: {
          'Content-Type': 'application/json'
      },
    auth: {
    user : 'USERID',
    pass : '****'
          },
    body: JSON.stringify("1": "T1111")
}
request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
           res.render('CRUD/singleselect', {
            title: 'Select',
            name: 'You!',
            result: body
  });
    }
    else
    {
        console.log(response.statusCode);
        req.flash('errors', { msg: 'Error during select' });
    }
})

实际响应是

{"ResultSet Output":[{"EMP_ID":"T1111","EMP_NAME":"ABC","DESIGNATION":"ENGINEER","REGISTRATION":"YES"}],"StatusDescription":"Execution Successful","StatusCode":200}

singleselect.jade

extends ../layout

block content
  .page-header
    table
      thead
    tbody
     table.table.table-striped.table-bordered.table-hover.table-condensed
      tr
        th EMPID
        th EMPNAME
        th DESIGNATION
         - var cnt = 1;
      each key, ind in result
        td= (cnt++)+'.'
        td= key
        tr

当我这样做时,我得到的响应是这样的

1.  {
2.  "
3.  R
4.  e
5.  s
6.  u
7.  l
8.  t
9.  S
10. e
11. t
12. 
13. O
14. u
15. t
16. p
17. u
18. t
19. "
20. :
21. [
22. {
23. "

我想要这样的输出

S.No EMPID EMPNAME DESIGNATION
1     T1111 ABC   ENGINEER  

请帮忙

我认为问题在于您正在设置字符串响应。你应该试试这个:(将它解析为 json 和 select "ResultSet Output"results 因为你直接循环遍历这个 属性.

       res.render('CRUD/singleselect', {
        title: 'Select',
        name: 'You!',
        result: JSON.parse(body)['ResultSet Output']
       });

我是一个 handelbars 人,但我强烈关注应该有用。

extends ../layout

block content
  .page-header
    table
      thead
    tbody
     table.table.table-striped.table-bordered.table-hover.table-condensed
      tr
        th EMPID
        th EMPNAME
        th DESIGNATION
         - var cnt = 1;
      each record, index in result
        td= record.EMP_ID
        td= record.EMP_NAME
        td= record.DESIGNATION
        tr

希望对您有所帮助。