【问题标题】:Use a ping to determine an IF statement using Javascript?使用 ping 来确定使用 Javascript 的 IF 语句?
【发布时间】:2012-08-09 20:07:28
【问题描述】:

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

这适用于移动浏览器(在本例中为 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>

【问题讨论】:

标签: javascript ajax if-statement ip ping


【解决方案1】:

网址不能只是一个 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 编码的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多