2012-08-13 28 views
0

我的這些論壇的第一篇文章:)使用ping來確定使用Javascript的IF語句?

這是在移動瀏覽器(在這種情況下android)工作。在知道使用哪個功能之前,我需要檢查計算機是否處於開啓狀態。

有沒有一種方法可以確定它的網絡狀態並運行相應的功能?現在,我只是有測試的消息。 (navigator.notification.alert是用於在移動設備上顯示消息的科爾多瓦方法)

我已經編好了這段代碼,有沒有什麼辦法可以使這個工作,我做了一些谷歌搜索無濟於事。

請注意,這是在本地網絡上工作。

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> 
+0

那些代碼不起作用?它是否在控制檯中給出任何錯誤? – sachleen 2012-08-13 15:01:12

+0

可能重複的[Javascript:檢查服務器是否在線?](http://stackoverflow.com/questions/5224197/javascript-check-if-server-is-online) – sachleen 2012-08-13 15:01:56

+0

這是另一個SO相關的問題從JS :http://stackoverflow.com/questions/4954741/how-to-ping-ip-addresses-using-javascript – jwatts1980 2012-08-13 15:02:54

回答

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編碼的結果。