ここに10秒ごとにGoogleにpingをかけ、htmlのMonitorInformationエレメントへの接続ステータスを表示するJavaScriptコードです。しかし、デバッグするhtmlファイルをクリックすると、MonitorInformation要素に表示される情報は常に「接続中です...待機しています」。私はしばらくの間デバッグしましたが、わかりません。どのようなアイデアが私のコードに間違っていますか?javascriptはウェブサイトのコードに接続していません
HTMLコード:
<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スクリプトコード:
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:私の修正JSON
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>
を使用して事前のおかげで、 ジョージ
JSONは機能しません。JSONPを使用する必要があります。JSONPは、当サービスが提供している場合にのみ機能します。 – FlySwat
FlySwatが混乱していますが、JSONを使用してEDIT 1セクションに投稿したコードが動作します。コメントはありますか? – George2