我们尝试检查身份验证。我们有用户名和密码。如果net_Work在mobile.authentication中可用,我们需要在线检查。互联网不在移动我们需要显示警报首先连接互联网 Ajax调用身份验证检查: -
function authentication()
{
username=$("#name").val();
password=$("#psw").val();
$('#empty').append("Authentication Will be Checking.......");
$.ajax({
url: 'http://183.82.0.221:1234/MyService.svc/GetAuthenticatedUserData/'+username+'/'+password+'',
dataType: 'jsonp',
type:'get',
cache:false,
success:function(data) {
if(data==1)
{
first();
}
else
{
alert("Authentication Failed");
}
}
});
}
以上代码仅在互联网有mobile.it工作正常的情况下。如果移动设备没有互联网,那么任何事情都没有响应。
如果手机没有互联网,我们需要显示提醒("此应用需要互联网,所以请先连接");
答案 0 :(得分:2)
如果您使用的是html5,那么解决方案非常简单。在javascript中有一个属性
navigator.onLine
通过使用此属性,您可以检查是否存在互联网连接。如果有互联网连接,则进行ajax调用,否则将数据存储在浏览器localstorage中,并在应用程序连接到互联网时将其推送回服务器。
现在如果您正在使用html4,那么解决方案将有点棘手。在ajax调用中还有一个事件监听器是“错误”。因此,您可以通过以这种方式对代码进行协调来进行脱机工作。
error: function(x, t, m) {
if(t==="timeout") {
alert("Means there is no internet connectivity");
now_run_your_offline_function()
} else {
alert(t);
}
这里可以是“超时”,“错误”,“未修改”和“parsererror”
如果您正在使用手机间隙。请检查此解决方案。 Android 2.3 and Phonegap, navigator.online does not work
在你的情况下,最终的代码将是。
function authentication()
{
username=$("#name").val();
password=$("#psw").val();
$('#empty').append("Authentication Will be Checking.......");
$.ajax({
url: 'http://183.82.0.221:1234/MyService.svc/GetAuthenticatedUserData/'+username+'/'+password+'',
dataType: 'jsonp',
type:'get',
cache:false,
timeout: 1000,
error: function(x, t, m) {
if(t==="timeout") {
alert("Means there is no internet connectivity");
} else {
alert(t);
}},
success:function(data) {
if(data==1)
{
first();
}
else
{
alert("Authentication Failed");
}
}
}); }