Created
March 12, 2017 14:14
-
-
Save afzafri/3ad6656af071f9ebe001a3024688a24e to your computer and use it in GitHub Desktop.
JS Code snippet for Ajax login function for Cordova/Hybrid apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// login function | |
function ajaxLogin(params) | |
{ | |
$.ajax({ | |
url: APIUrl+'/login', | |
type: 'post', | |
dataType: 'json', | |
data: params, | |
success: function(data) { | |
// 200 = success login, 400 = wrong credientials | |
if(data['code'] == 200) | |
{ | |
// store user info into html5 local storage | |
localStorage.userID = data['user_data']['id']; | |
localStorage.userName = data['user_data']['name']; | |
localStorage.userEmail = data['user_data']['email']; | |
localStorage.userRole = data['user_data']['role']; | |
localStorage.userPic = data['profile_pic']; | |
var res = params.split("&"); | |
var pass = (res[1].split("="))[1]; | |
localStorage.userPass = pass; | |
alert(data['message']); | |
// login success, redirect to Homepage/Dashboard | |
window.location="home.html"; | |
} | |
else | |
{ | |
alert(data['message']); | |
$('.se-pre-con').hide(); | |
} | |
}, | |
error: function (xhr, ajaxOptions, thrownError) { | |
alert("Connection Error! "+xhr.responseText); // <- same here, your own div, p, span, whatever you wish to use | |
$('.se-pre-con').hide(); | |
Materialize.toast("This application require internet connection", 4000); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment