如下代码,EventSource会把单词的前置空格吃掉,比如" blank" 会变成"blank",这会导致输出的英语单词连在一起
javascript
const eventSource = new EventSource(api_question);
eventSource.onmessage = streamEvent => {
const data = streamEvent.data;
handleTest(data);
};
临时解决方案,将后端的数据包装成json,前端再解析出来。
javascript
eventSource.onmessage = streamEvent => {
const data = JSON.parse(streamEvent.data);
resText += data.val;
handleTest(resText);
};