2017-05-24 76 views
0

繼續從這question繼續,我設法將我的chatbot的對話歷史記錄保存爲一個扁平的json文件。我現在想看看index.html文件中的json的內容。我想在我的HTML使用此代碼,但我得到了一個空白頁面從平板JSON數據檢索到HTML

<html> 
<head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 

    <script>  
    $(function() { 
     $.getJSON('chathistory.json', function(json) { 
      console.log(json); 
     }); 
    }); 
    </script> 
</head> 
</html> 

我的聊天機器人的對話記錄代碼:

bot.use({ 
    botbuilder: function (session, next) { 
     //console.log(session.message.text); 
     test="\r\nUSER: "+session.message.text; 
     fs.appendFile(filename, test, function(err){  }); 
     next(); 
    }, 

    send: function (event, next) { 
     //console.log(event.text); 
     test="\r\nBOT: "+event.text; 
     fs.appendFile(filename, test, function(err){  }); 
     next(); 
    } 
}); 

我chathistory.json文件:

USER: hi 
BOT: Hi, I am your chatbot. 
BOT: What is your name? 
USER: Anish 
BOT: Hello, Anish. How may I help you today? 

如何在我的index.html頁面中看到這些數據?

+1

確定您收到一個空白頁面,但您是否檢查了開發人員控制檯以查看您的json是否已從文本文件正確讀取?你正在得到一個空白頁面,因爲你的JavaScript沒有對json數據做任何事情。 – victor

回答

0

你正在做console.log(json)所以你不寫任何東西在HTML中;嘗試做document.write(json)

+0

這不適合我。我仍然得到一個空白頁面。你認爲我的HTML代碼的其餘部分是正確的嗎? – Anish

+0

你確定你正在擊球嗎?也許getJson失敗了? –

+0

'chathistory.json'是一個已經在bot的相同文件夾中創建的文件,以及app.js,package.json等等。所以我仍然需要提及整個路徑或者chathistory.json工作正常?此外,該文件是相當平坦的沒有任何json格式。更像是自由文本。會導致任何問題嗎? – Anish