0
我正在寫一個Node.js服務器,我試圖從API中獲取數據並將其返回給我的用戶。我正在採取insightlyResponse
並嘗試轉換爲JSON。這是我的代碼:爲什麼數據解析失敗?
insightlyResponse.setEncoding('utf8');
let rawData = '';
insightlyResponse.on('data', (chunk) => rawData += chunk);
insightlyResponse.on('end',() => {
try {
const parsedData = JSON.parse(rawData);
responseData = "PARSED";
} catch (e) {
responseData = `Got error: ${e.message}`
}
response.end(responseData);
});
錯誤是Got error: Unexpected token \u001f in JSON at position 0
。這是什麼意思,我做錯了什麼?
「試圖轉換爲JSON」我想你的意思是「試圖轉換_from_ JSON」。 'rawData'中有什麼?大概不是JSON。 – smarx
@smarx我正在如下:https://stackoverflow.com/questions/11826384/calling-a-json-api-with-node-js –
如果我不解析並按原樣離開,我會得到一堆亂碼,如果我解析我得到一個錯誤。 –