2016-03-08 100 views
0

我正在尝试使用一个node.js应用程序,我正在发送一个POST请求。响应是json格式,我需要在jade页面中显示它。当我尝试这样做时,我没有得到正确的JSON响应。如何将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 

请帮

回答

0

我认为这个问题是您正在设置一个字符串响应。你应该试试这个:(它解析成JSON并选择"ResultSet Output"results你直接循环throught该物业

 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 

。希望这有帮助

+0

嗨戴夫,感谢您的投入,但现在我得到的回应是[对象对象] – Kirthika

+0

这意味着你在正确的轨道上,只需要相应地修改你的'视图'给我一分钟,我会更新 答案。 –

+0

谢谢一吨戴夫。工作就像一个魅力:) – Kirthika