在客户端如何实现这个无刷新的需求?

shadan05 2012-08-23 02:59:51
需求是这样的:

我一套winform应用程序,是某个设备操作的应用程序, 现在想通过浏览器方式去操作设备。

目前我的实现方式系统启动时候使用HttpListener创建一个web服务器,让后客户端访问这个服务器加一些url参数,然后我通过这些url参数判断,处理当次的请求,处理完成后返回客户端需要显示的html。

/// <summary>
/// 处理web请求
/// </summary>
/// <param name="context"></param>
public void ProcessRequst(HttpListenerContext context)
{
//得到请求
if (context.Response != null)
{
//得到html
string strHtml = ControlManager(context);
//返回客户端的html
if (!string.IsNullOrEmpty(strHtml))
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strHtml);
context.Response.ContentLength64 = buffer.Length;
context.Response.KeepAlive = false;
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Flush();
context.Response.OutputStream.Close();
}
}
}



到这里客户端访问是没有问题,效果也不错。
让我头疼的问题是:
我在设备上面操作了,我改咋样将客户端的页面同步到当前的操作页面呢?
第一种方式: 客户端定时的刷新,一直去请求我在服务器端做处理然后返回客户端需要显示的页面,但是这种刷新一闪一闪的,效果不好。
第二种方式: 使用ajax去实现无刷新效果,但是折腾了半天,貌似不行原因是访问到xmlhttp.responseText就提示xmlhttp没有定义,调试了下提示这个对象还不可以使用,代码如下:

function RequestTest() {

var xmlHttp;
var url = "";

if (window.ActiveXObject) {

xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}

xmlHttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert(xmlHttp.responseText);
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
}

xmlHttp.open("GET", url, true);
xmlHttp.send();

}



还有没其他的方法,或者说ajax是我写的不对?还是咋样?求帮助
...全文
208 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
shadan05 2012-08-23
  • 打赏
  • 举报
回复
问题解决了, 主要是不太了解这个机制,现在完全弄明白了, url没错是因为缓存了页面,所以没有去请求。

感谢给为。

  • 打赏
  • 举报
回复
var url = "http://xxx.xxx.xxx.xxx:80?time=0";

不能跨域。。确认你访问网站的域名也是xxx.xxx.xxx.xxx,要不就跨域了。。最好改成绝对路径

参考这个http://topic.csdn.net/u/20120820/21/6685a3d1-8761-4091-b3a0-e3b62d436a13.html
shadan05 2012-08-23
  • 打赏
  • 举报
回复
先感谢下俩问,

我弄个页面试试先。
未知数 2012-08-23
  • 打赏
  • 举报
回复
var url = "http://xxx.xxx.xxx.xxx:80?time=0";
地址栏参数用?
还有url中感觉差了个页面地址

shadan05 2012-08-23
  • 打赏
  • 举报
回复
是因为url的问题吗?
shadan05 2012-08-23
  • 打赏
  • 举报
回复
还有问题是:


function RequestTest() {

var xmlHttp;
var url = "http://xxx.xxx.xxx.xxx:80/time=0";

if (window.ActiveXObject) {

xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}

xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
document.getElementById("content").innerHTML = xmlHttp.responseText;
setTimeout("RequestTest() ", 2000);
}
}
}
xmlHttp.open("GET", url, true);
xmlHttp.send();

}


不知道为什么服务器收不到请求呢?
shadan05 2012-08-23
  • 打赏
  • 举报
回复
这个是copy的,这个我修改了, 抱歉了,之前可能是找其他的问题,不知道什么时候修改了。

未知数 2012-08-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
//document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
document.getElementById("myDiv").innerHTML = xmlHttp.responseText;
[/Quote]
对 ,变量名写错,为什么会是这种错呢。。。输入时有提示的吧,除非是copy的代码
  • 打赏
  • 举报
回复
//document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
document.getElementById("myDiv").innerHTML = xmlHttp.responseText;
shadan05 2012-08-23
  • 打赏
  • 举报
回复
自己顶下。

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧