2017-05-24 69 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