Hier ist mein Javascript-Code, der Google für alle 10 Sekunden ping und den Verbindungsstatus zu HTML MonitorInformation Element anzeigen. Aber wenn ich auf die HTML-Datei klicke, um zu debuggen, ist die Information, die am MonitorInformation Element angezeigt wird, immer "Verbinden ... warten". Ich habe für einige Zeit debugged, kann aber nicht herausfinden. Irgendwelche Ideen was ist falsch mit meinem Code?Javascript Verbindung mit Website-Code funktioniert nicht
Html-Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Monitor.js" type="text/javascript"></script>
<title>Web Site Monitor</title>
</head>
<body onload="setup()">
<div id="MonitorInformation">Connecting...wait</div>
</body>
</html>
Java-Script-Code:
function setup() {
window.setInterval(PingWebSite, (10 * 1000));
}
function PingWebSite() {
conObj = new ActiveXObject("Msxml2.XMLHTTP");
conObj.open("GET", "http://www.google.com", true);
conObj.onreadystatechange = function() {
if (conObj.readyState === 4) {
if (conObj.status === 200) {
loading.innerText = "Service is available";
} else {
MonitorInformation.innerText = "Service is not available";
}
} else {
MonitorInformation.innerText = "Connecting to www.google.com ...";
}
}
}
EDIT 1: meine fix JSON mit
function setup() {
window.setInterval(PingWebSite, (10 * 1000));
}
function PingWebSite() {
var http_request = new XMLHttpRequest();
http_request.open("GET", "http://www.google.com", true);
http_request.send(null);
http_request.onreadystatechange = function() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
MonitorInformation.innerText = "Connection ok";
alert("ok");
} else {
MonitorInformation.innerText = "Connection fail";
alert("fail");
}
http_request = null;
}
};
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Monitor.js" type="text/javascript"></script>
<title>Web Site Monitor</title>
</head>
<body onload="setup()">
<div id="MonitorInformation">Connecting...wait</div>
</body>
</html>
Vielen Dank im Voraus, George
JSON wird nicht funktionieren, Sie müssen JSONP verwenden, das funktioniert nur, wenn der Dienst, den Sie treffen, es bereitstellt. – FlySwat
Ich bin verwirrt FlySwat, aber mein Posted Code in EDIT 1 Abschnitt mit JSON funktioniert. Irgendwelche Kommentare? – George2