问题
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".
For some reason Javascript is not accepting the UTF-8 text sent by the ASP script.
Help me please!!!
The asp 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
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 :(
}
Thank you!
回答1:
Try to use Response.Flush
after Response.Write
Edit*
Put two enters after the strResponse string:
response.Write strResponse & vbCrLf & vbCrLf
回答2:
Try This Is Working
Response.Write("data: The server time is: " & now()) & chr(13) & chr(10) & chr(13) & chr(10)
Response.Flush()
来源:https://stackoverflow.com/questions/36339704/server-sent-event-and-asp-classic