Javascript / AJAX请求不适合在IE / chrome中正常工作

时间:2022-11-05 14:25:55

I am writing a simple PHP/JS script to determine and manage status of all windows terminal servers. As you know the access via WMI is very slow so I want AJAX to show the progress of each server when he is looping every server. The code works as expected in firefox. In chrome the code works, but does not show a status while looping. After the loop the content is displayed. In IE11 the code does not work. Checking by alerts IE calls both functions and also gets in if statement creating requestobject. Can somebody help me to get IE working and Chrome showing status? Thank you for your help.

我正在编写一个简单的PHP / JS脚本来确定和管理所有Windows终端服务器的状态。如您所知,通过WMI访问非常慢,因此我希望AJAX在循环每个服务器时显示每个服务器的进度。代码在firefox中按预期工作。在chrome中,代码可以工作,但在循环时不显示状态。循环之后显示内容。在IE11中,代码不起作用。通过警报检查IE会调用这两个函数,并且还会在if语句中创建requestobject。有人可以帮助我让IE工作,Chrome显示状态吗?谢谢您的帮助。

My code looks like this:

我的代码如下所示:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="tsmanager.css">
<script type="text/javascript">
	function refreshall()
	{
		var table = document.getElementById("terminalservers");
		for (var i = 0, row; row = table.rows[i]; i++)
		{
			var sComputer = row.id;
			if (sComputer != "")
			{
				setTimeout(refresh(sComputer), 2000);	
				setTimeout(document.getElementById("turn").innerHTML = sComputer, 2000);	
			}

		}

	}
	
	function refresh(sComputer)
	{
		if (window.XMLHttpRequest)
		{
			req = new XMLHttpRequest();
		}
		else 
		{
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
		req.onreadystatechange = function()
		{
			if (req.readyState == 4 && req.status == 200)
			{
				document.getElementById(sComputer).innerHTML = req.responseText;
			}
		}
		req.open("GET", "serverrequest.php?name="+sComputer, false);
		req.send();
	}
	
</script>
</head>
<body>
<table id="terminalservers">

  /* some table content with row id=servername */


</table>	
<button onclick="refreshall()">alle</button>
<br><span id="turn">leer</span>
</body>
</html>

1 个解决方案

#1


0  

I would suggest you look at using jQuery - it provides simple and robust cross-browser support for AJAX and results in much more readable code on all fronts.

我建议你看一下使用jQuery - 它为AJAX提供简单而强大的跨浏览器支持,并在所有方面产生更易读的代码。

#1


0  

I would suggest you look at using jQuery - it provides simple and robust cross-browser support for AJAX and results in much more readable code on all fronts.

我建议你看一下使用jQuery - 它为AJAX提供简单而强大的跨浏览器支持,并在所有方面产生更易读的代码。