Reputation: 1276
The following code submits data to the server. The data is actually stored in the localStorage before submit to server. This is due to my requirement for the user to be able to use my system without an internet connection.
The problem is that when there is no internet connection, obviously it cant post the data to the server. What should I do in this situation?
$('#btnSync').on('click', function(e) {
var my_data = {};
var my_value = [];
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
if((localStorage.key( i ).includes('set'))){
var lsItems = JSON.parse(localStorage.getItem( localStorage.key( i ) ));
allItems[localStorage.key( i )] = lsItems;
}
}
$.ajax({
url: "action.php",
type: "POST",
data: {'action':'btnSync','allItems':allItems},
dataType: "json",
success: function(data) {
$.notify(
{
message: data.message
},
{
type: data.status,
placement: {
from: "top",
align: "left"
},
newest_on_top: true,
delay: 0
}
);
},
error: function(data){
$.notify(
{
message: 'Unexpected error occured, please kindly contact System Administrator'
},
{
type: 'danger',
placement: {
from: "top",
align: "left"
},
newest_on_top: true,
delay: 0
}
);
}
});
});
Upvotes: 1
Views: 3023
Reputation: 799
you can check whether connection/server is up using a similar ajax call. Look for
statusCode.status == 0
in error and give destination unreachable/Internet down notice to user. You can create this ajax call as a promise in a function and use that before all your ajax calls, and ask user to connect to the Internet then retry.
Upvotes: 2