如何通过ajax调用检查网络连接

时间:2022-03-11 07:16:17

We tried to check authentication.We have username and password.We need if net_Work is available in mobile.authentication check through online. internet is not in mobile We need show alert first connected internet Ajax call authentication check :-

我们试图检查身份验证。我们有用户名和密码。我们需要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");
 }
}
});
}

Above code is only if internet have mobile.it's working fine. If mobile don't have internet it's not response any thing.

上面的代码只有互联网有mobile.it工作正常。如果手机没有互联网,那就没有任何反应。

if mobile don't have internet, We need Show alert("this app need internet so Please connect first");

如果手机没有互联网,我们需要显示提醒(“此应用需要互联网,所以请先连接”);

1 个解决方案

#1


2  

If you are using html5 then the solution is very simple.There is a property in javascript

如果您使用的是html5,那么解决方案非常简单。在javascript中有一个属性

 navigator.onLine

by using this property you can check that there is internet connectivity or not.If there is internet connectivity then make ajax call else store your data in browser localstorage and push it back to server when application get internet connected.

通过使用此属性,您可以检查是否存在互联网连接。如果有互联网连接,则进行ajax调用,否则将数据存储在浏览器localstorage中,并在应用程序连接到互联网时将其推送回服务器。

Now If you are using html4 then the solution will be little bit tricky.In ajax call there is one more event listener that is "error".So you can make offline work by righting code in such a manner.

现在如果您使用的是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);
    }

Here t can be "timeout", "error", "notmodified" and "parsererror"

这里t可以是“超时”,“错误”,“未修改”和“parsererror”

In case you are using phone gap.Then check this solution. Android 2.3 and Phonegap, navigator.online does not work

如果您使用手机间隙。然后检查此解决方案。 Android 2.3和Phonegap,navigator.online不起作用

In your case final code will be.

在你的情况下,最终的代码将是。

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");
}
}

}); }

#1


2  

If you are using html5 then the solution is very simple.There is a property in javascript

如果您使用的是html5,那么解决方案非常简单。在javascript中有一个属性

 navigator.onLine

by using this property you can check that there is internet connectivity or not.If there is internet connectivity then make ajax call else store your data in browser localstorage and push it back to server when application get internet connected.

通过使用此属性,您可以检查是否存在互联网连接。如果有互联网连接,则进行ajax调用,否则将数据存储在浏览器localstorage中,并在应用程序连接到互联网时将其推送回服务器。

Now If you are using html4 then the solution will be little bit tricky.In ajax call there is one more event listener that is "error".So you can make offline work by righting code in such a manner.

现在如果您使用的是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);
    }

Here t can be "timeout", "error", "notmodified" and "parsererror"

这里t可以是“超时”,“错误”,“未修改”和“parsererror”

In case you are using phone gap.Then check this solution. Android 2.3 and Phonegap, navigator.online does not work

如果您使用手机间隙。然后检查此解决方案。 Android 2.3和Phonegap,navigator.online不起作用

In your case final code will be.

在你的情况下,最终的代码将是。

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");
}
}

}); }