我制作了一个带有服务器发送事件所需头文件的ASP脚本。
客户端脚本正在工作。我用PHP脚本测试过它,它很好用。
如果你从URL行调用ASP脚本,它会返回预期的消息,但是当javascript调用它时,元素检查器将数据返回为“undefined”。
当我将断点设置为“open”事件监听器时,“e.data”就是“undefined”。
由于某种原因,Javascript无法接受ASP脚本发送的UTF-8文本。
请帮帮我!!!
ASP代码:
JavaScript代码:
谢谢你!
由于某种原因,Javascript无法接受ASP脚本发送的UTF-8文本。
请帮帮我!!!
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代码:
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 :(
}
谢谢你!
code
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 - undefinedstrResponse
的部分,否则它将不包含除了"data:"和当前时间之外的任何内容 - 这是您希望它包含的全部吗? - undefinedvar source = new EventSource('http://yourdomain.com/serversend.asp');
,会发生什么? - undefined