问题描述
我制作了一个带有服务器发送事件所需的所有标头的ASP脚本.客户端脚本正在运行.我使用PHP脚本对其进行了测试,并且效果很好.ASP脚本可以工作,并且如果您从URL行调用它,它会返回预期的消息,但是当javascript调用它时,元素检查器将数据返回为未定义".当我将断点设置为打开"事件侦听器时,"e.data"为未定义".
I made a ASP script with all headers necessary for Server Sent Events.The client side script is working. I tested it with a PHP script and it works fine.The ASP script works and it returns the expected message if you call it from the URL line, but when the javascript calls it, the element inspector returns the data as "undefined".When I set the breakpoint to the "open" event listener, the "e.data" is "undefined".
由于某种原因,Javascript不接受ASP脚本发送的UTF-8文本.
For some reason Javascript is not accepting the UTF-8 text sent by the ASP script.
请帮助我!
asp代码:
strResponse = strResponse & "data: " & now()
response.Buffer = true
response.CharSet = "utf-8"
response.ContentType="text/event-stream"
response.AddHeader "content-Type", "text/event-stream;charset=UTF-8"
response.AddHeader "Cache-Control", "no-cache"
response.AddHeader "Content-Length", len(strResponse)
response.AddHeader "Connection", "Keep-Alive"
response.Write strResponse
JavaScript:
The Javascript:
if (!!window.EventSource) {
var source = new EventSource('serversend.asp');
source.addEventListener('message', function (e) {
console.log(e.data);
}, false);
source.addEventListener('open', function (e) {
console.log("open " + e.data);
}, false);
source.addEventListener('error', function (e) {
if (e.readyState == EventSource.CLOSED) {
console.log(e.data);
}
}, false);
} else {
// something else :(
}
谢谢!
推荐答案
在Response.Write
编辑*在strResponse字符串后放置两个回车:response.Write strResponse & vbCrLf & vbCrLf
Edit*Put two enters after the strResponse string:response.Write strResponse & vbCrLf & vbCrLf
这篇关于服务器发送事件和ASP经典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!