2017-07-28 92 views
1

我想弄清楚是否有一種方法可以在node.js中的JSON響應中發送數組。我已經在它們是數組的響應中附加了dataPoints1,dataPoints2和dataPoints3。但是,當它是從一個Ajax請求接收時,它只是變成一個字符串,它是[對象的對象],[對象的對象]Node.js有沒有辦法在JSON響應中發送數組?

responsearray = '{"result":{"System":"Ready","Allcompleted":"completed","dataPoints1":"'+dataPoints1+'","dataPoints2":"'+dataPoints2+'","dataPoints3":"'+dataPoints3+'"}}'; 
res.setHeader('Content-Type', 'application/json'); 
res.type('application/json'); 
    res.send(responsearray); 

有,我可以一個JSON串內從Ajax發送的陣列的方式調用然後把它轉回到一個數組? 謝謝

回答

1

.send方法接受JavaScript對象作爲參數。你不需要建立一個字符串。

嘗試用:

var responsearray = { 
    result: { 
    System: "Ready", 
    Allcompleted: "completed", 
    dataPoints1: dataPoints1, 
    dataPoints2: dataPoints2, 
    dataPoints3: dataPoints3 
    } 
}; 

/// ... 

res.send(responsearray); 

編號:http://expressjs.com/en/api.html#res.send

+0

我認爲它的工作原理,謝謝!明天早上需要重新確認,但看起來像它的工作。 – Benyaman

相關問題