服务器已发送事件无法在Google Chrome中使用 [英] Server Sent Event not working in google chrome

查看:192
本文介绍了服务器已发送事件无法在Google Chrome中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的服务器JSP代码Server_Date.jsp

This is my server JSP code "Server_Date.jsp"

<%
response.setHeader("cache-control", "no-cache"); 
response.setContentType("text/event-stream");
out.print("data: " + (new java.util.Date()).toString() + "x\n\n");
out.flush();   
%>

我的客户端jsp页面Client_Serverdate.jsp

This my client jsp page "Client_Serverdate.jsp"

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body  onload="begin()">
<h1>Getting server updates</h1>
<div id="result"></div>
<script >
if(typeof(EventSource)!=="undefined")
{     
var source=new EventSource("Server_Date.jsp");      
source.addEventListener("message", function(event) {
    document.getElementById("result").innerHTML = event.data;
}, false);
}
else
{
document.getElementById("result").innerHTML="Sorry, your browser does not support server-sent events...";
}
</script>   
</body>
</html>

请帮我这个代码在Mozilla Firefox中工作正常,歌剧但不在Google Chrome中工作我是用18.x和20.x检查

Please help me this code is working fine in Mozilla Firefox, opera but not working in Google chrome i was checked with 18.x and 20.x also

它将进入我用print语句检查的服务器页面,但它没有到达document.getElementById行结果)。innerHTML = event.data;

It is going to server page i have checked with print statement but its not coming to the line "document.getElementById("result").innerHTML = event.data;"

谢谢......

推荐答案

问题解决了

解决方案:

页面编码问题:客户端使用UTF-8编码

Page encoding problem : client side used UTF-8 Encoding

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

未提及服务器端UTF-8,因此在setContentType中添加charset = UTF-8后

Server side UTF-8 was not mention, so after adding charset=UTF-8 in setContentType its working

response.setContentType("text/event-stream;charset=UTF-8");

感谢大家努力回答我的问题

Thanks for people take your effort to answer my question

这篇关于服务器已发送事件无法在Google Chrome中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆