0

我在这些论坛的第一篇文章 :)

这适用于移动浏览器(在这种情况下为 android)。在我知道要使用哪个功能之前,我需要检查计算机是否打开。

有没有办法可以 ping 它以确定它的网络存在并运行相应的功能?现在,我只有要测试的消息。(navigator.notification.alert 是用于在移动设备上显示消息的 Cordova 方法)

我已经编写了这段代码,有什么办法可以让它工作,我做了一些谷歌搜索无济于事。

请注意,这是在本地网络上工作的。

function isOn() {
    $.ajax({
        url : 'http://192.168.x.xxx' ,              //x.xxx being the client ip
        success : function () {
            navigator.notification.alert(
        'The computer is on.',              //Message body
        alertDismissed,                 //Callback
        'The Computer is On',               //Title
        'Yes'                       //Button Label
        );
        },
        error : function () {
            navigator.notification.alert(
            'The computer is off.',         //Message body
            alertDismissed,             //Callback
            'The Computer is Off',          //Title
            'Yes'                   //Button Label
            );
        }
    });
};


<a onclick="isOn();">Test</a>
4

1 回答 1

0

url 不能只是一个 IP。您必须索取文件。如果您的客户端没有可用的 Web 服务器,您将收到错误消息。

因此,如果要 ping 计算机,则必须调用服务器端脚本:

$.post('http://myserver.com/ping.php', {'ip': '111.222.123.123'}, function(data){
  // your code
});

在服务器端,您可以使用以下内容: http:
//www.codediesel.com/php/ping-a-server-using-php/ ping 客户端,然后返回 JSON 编码的结果。

于 2012-08-13T15:10:38.840 回答