2016-03-08 64 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