Windows 7 小工具 AJAX 停止工作
我正在编写一个小小工具,它将显示我的游戏服务器的当前状态。 它通过返回 JSON 的 URL 获取信息。一开始效果很好。但现在似乎卡在AJAX调用上。
这是我的脚本:
function start() {
$("body").css({
height: "100px",
width: "130px",
margin: 0
});
$("#refresh").click(function (event) {
event.preventDefault();
fetch_info();
});
fetch_info();
};
function fetch_info() {
var now = new Date();
var msec = now.getTime();
ajax = new ActiveXObject("Msxml2.XMLHTTP");
ajax.open("GET", "http://the.amazing.url?msec="+msec, true);
ajax.onreadystatechange = function() {
if (ajax.readyState === 4) {
if (ajax.status === 200) {
var json = $.parseJSON(ajax.responseText);
if (json.map != "error")
{
$("#map").html(json.map);
$("#mode").html(json.gamemode);
$("#current").html(json.players);
$("#max").html(json.max_players);
}
else
{
$("#map").html("crashed");
}
} else {
$("#map").html("error");
}
}
else
{
$("#map").html("fetching data");
}
}
ajax.send(null);
}
当小部件启动时,它显示“正在获取数据”,但之后什么也没有发生。它获取有效的 JSON 数据。
有谁知道为什么或调试这个脚本的方法?
I am writing a little Gadget that will show me the current status of my game server.
It gets its information via an URL that returns JSON. At the beginning it worked fine. But now it seems to be stuck at the AJAX call.
This is my script:
function start() {
$("body").css({
height: "100px",
width: "130px",
margin: 0
});
$("#refresh").click(function (event) {
event.preventDefault();
fetch_info();
});
fetch_info();
};
function fetch_info() {
var now = new Date();
var msec = now.getTime();
ajax = new ActiveXObject("Msxml2.XMLHTTP");
ajax.open("GET", "http://the.amazing.url?msec="+msec, true);
ajax.onreadystatechange = function() {
if (ajax.readyState === 4) {
if (ajax.status === 200) {
var json = $.parseJSON(ajax.responseText);
if (json.map != "error")
{
$("#map").html(json.map);
$("#mode").html(json.gamemode);
$("#current").html(json.players);
$("#max").html(json.max_players);
}
else
{
$("#map").html("crashed");
}
} else {
$("#map").html("error");
}
}
else
{
$("#map").html("fetching data");
}
}
ajax.send(null);
}
When the widget starts it is showing "fetching data" but after that it happens nothing. It gets valid JSON Data.
Does anyone know why or a way to debug this script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两个分号错误。它们很可能是在您改进脚本期间发生的,并且分号被移动了。第 14 行不需要分号,但第 44 行需要。
第 14 行:
第 44 行:
对于调试脚本,我使用内置的浏览器开发工具,例如 IE 中的 F12 或 Chrome 中的开发工具。当我制作我的小工具时,我使用一个基本网页测试了所有脚本,以便我可以观看浏览器 JavaScript 控制台。
JSLint 也很有帮助。但是,我在使用时选择以下选项:
假设 Windows,容忍未使用的参数,容忍缺少“use strict”编译指示,容忍每个函数有许多 var 语句,并容忍混乱的空白。否则 JSLint 会因为一些无关紧要的事情而对你的脚本发疯。希望这有帮助。
另外,如果您有 Visual Studio,则可以使用其脚本调试器。这是一篇解释如何操作的文章: http://www.howtogeek.com/howto/windows-vista/how-to-debug-a-windows-vista-sidebar-gadget-with-visual-studio/
There are two semicolon errors. They most likely occurred during your improvement of the script and the semicolon got moved. Line 14 does not need a semicolon, but Line 44 does.
Line 14:
Line 44:
As for Debugging Scripts, I use the built in browser developer tools Like F12 in IE or Developer Tools in Chrome. When I was making my Gadget, I tested all the scripts with a basic webpage so that I could watch the browser javascript console.
JSLint is also helpful. However, I select the following options when using it:
Assume Windows, Tolerate unused parameters, Tolerate missing 'use strict' pragma, Tolerate many var statements per function, and Tolerate messy white space. Else JSLint will go crazy on your script over things that do not matter. Hope this helps.
Also if you have Visual Studio you can use its script debugger. Here is an article explaining how: http://www.howtogeek.com/howto/windows-vista/how-to-debug-a-windows-vista-sidebar-gadget-with-visual-studio/